package step.core; import java.util.*; parser code {: StepGenericRepository readRepository; ArrayList unresInstRefList = new ArrayList(); StepGenericRepository getReadRepository() { return readRepository; } void resolveInstanceReference(StepGenericInstanceReference instRef) { if (instRef != null) { StepObject refed = readRepository.instanceAt(instRef.getId()); if (refed == null) { unresInstRefList.add(instRef); } else { instRef.setVal(refed); } } } public static void main(String args[]) throws Exception { if (args.length==0) { new StepParser(new StepScanner(System.in)).parse(); } else { new StepParser(new StepScanner(new java.io.FileInputStream(args[0]))).parse(); } } public void parseFileNamed(String filename, StepGenericRepository repo) throws Exception { this.parseStream(new java.io.FileInputStream(filename), repo); } public void parseString(String source, StepGenericRepository repo) throws Exception { parseFromReader(new java.io.StringReader(source), repo); } public void parseStream(java.io.InputStream stream, StepGenericRepository repo) throws Exception { parseFromReader(new java.io.InputStreamReader(stream), repo); } public void parseFromReader(java.io.Reader reader, StepGenericRepository repo) throws Exception { setScanner(new StepScanner(reader)); parseIntoRepository(repo); } public void parseIntoRepository(StepGenericRepository repo) throws Exception { readRepository = repo; readRepository.clear(); parse(); ArrayList gotUnresInstRefList = unresInstRefList; unresInstRefList = new ArrayList(); for (int i = 0; i < gotUnresInstRefList.size(); i++) { StepGenericInstanceReference instRef = gotUnresInstRefList.get(i); resolveInstanceReference(instRef); } if (unresInstRefList.size() > 0) { System.out.println("Warning: there are some unresolved instances"); } /* if (unresInstRefList.size() > 0) { throw new StepExistingUnresolvedInstanceReferenceException(unresInstRefList); } */ } :} terminal LOGICAL_TRUE, LOGICAL_FALSE, LOGICAL_UNKNOWN, COMMA, DATA, ENDSCOPE, ENDSEC, END_ISO_10303_21, HEADER, LEFT_PAREN, RIGHT_PAREN, SCOPE, SEMI, SLASH, ISO_10303_21; terminal String KEYWORD, USER_DEFINED_KEYWORD, STRING, ENCODED_STRING, BINARY, ENUMERATION; terminal Integer INSTANCE_LABEL, INTEGER; terminal Double REAL; terminal StepValue MISSING, REDEFINE; terminal Integer INSTANCE_REF; non terminal DataSection, ScopeDataEntities, DataEntities, ScopeInstRefs, Entity_export; non terminal DataEntity, ScopeInstanceReference, ExchangeFile; non terminal ArrayList HeaderSection; non terminal ArrayList HeaderEntities, Subsuper_Record, Subsuper_Record_List, Parameters; non terminal StepAggregationValue EmbeddedList, List; non terminal StepHeaderInstance HeaderEntity; non terminal StepGenericInstance Entity_Instance_RHS; non terminal Integer InstLabel; non terminal String HeaderEntityName; non terminal StepSimpleRecord Simple_Record, Subsuper_Record_List_Element; non terminal StepValue Parameter, TypedValue, ListEntry, InstRef, InstanceReference; ExchangeFile ::= ISO_10303_21 HeaderSection:hs DataSection END_ISO_10303_21 {: parser.getReadRepository().addAllHeaderInstances((ArrayList)hs); :} ; HeaderSection ::= HEADER HeaderEntities:he ENDSEC {: RESULT = he; :} ; HeaderEntities ::= HeaderEntities:hes HeaderEntity:he {: hes.add(he); RESULT = hes; :} | {: RESULT = new ArrayList(); :} ; DataSection ::= DATA DataEntities ENDSEC ; DataEntities ::= | DataEntities:des DataEntity:de ; ScopeDataEntities ::= DataEntity | ScopeDataEntities DataEntity ; DataEntity ::= InstLabel:l Entity_Instance_RHS:i SEMI {: parser.getReadRepository().record(i,l); :} | InstLabel:l SCOPE ScopeDataEntities ENDSCOPE Entity_export Entity_Instance_RHS:i SEMI {: RESULT = i; parser.getReadRepository().record(i,l); :} ; InstLabel ::= INSTANCE_LABEL:l {: RESULT = l; :} ; InstRef ::= INSTANCE_REF:ir {: StepGenericInstanceReference instRef = new StepGenericInstanceReference(ir.intValue()); parser.resolveInstanceReference(instRef); RESULT = instRef; :} ; HeaderEntityName ::= KEYWORD:kw {: RESULT = kw; :} | USER_DEFINED_KEYWORD:kw {: RESULT = kw; :} ; HeaderEntity ::= HeaderEntityName:n LEFT_PAREN Parameters:p RIGHT_PAREN SEMI {: RESULT = new StepHeaderInstance(n,p); :} ; /* right-hand-side minus the Scope, that is */ Entity_Instance_RHS ::= Simple_Record:r {: RESULT = new StepInternalRepresentation(r.getEntityName(),r.getValues()); :} | Subsuper_Record:l {: RESULT = new StepExternalRepresentation(); ((StepExternalRepresentation)RESULT).addAllRecords((ArrayList)l); :} ; Subsuper_Record ::= LEFT_PAREN Subsuper_Record_List:l RIGHT_PAREN {: RESULT = l; :} ; Subsuper_Record_List ::= Subsuper_Record_List_Element:e {: RESULT = new ArrayList(); RESULT.add(e); :} | Subsuper_Record_List:l Subsuper_Record_List_Element:e {: RESULT = l; RESULT.add(e); :} ; /* a lot like simple_record, but entities are created with only explicit */ /* attributes */ Subsuper_Record_List_Element ::= KEYWORD:kw LEFT_PAREN Parameters:p RIGHT_PAREN {: RESULT = new StepSimpleRecord(kw, p); :} ; Simple_Record ::= KEYWORD:kw LEFT_PAREN Parameters:p RIGHT_PAREN {: RESULT = new StepSimpleRecord(kw,p); :} | USER_DEFINED_KEYWORD:kw LEFT_PAREN Parameters:p RIGHT_PAREN {: RESULT = new StepSimpleRecord(kw,p); :} ; TypedValue ::= KEYWORD:kw LEFT_PAREN Parameter:p RIGHT_PAREN {: RESULT = new StepTypedValue(kw, p); :} | USER_DEFINED_KEYWORD:kw LEFT_PAREN Parameter:p RIGHT_PAREN {: RESULT = new StepTypedValue(kw, p); :} ; Parameters ::= /* null */ {: RESULT = new ArrayList(); :} | Parameter:p {: RESULT = new ArrayList(); RESULT.add(p); :} | Parameters:ps COMMA Parameter:p {: ps.add(p); RESULT = ps; :} ; Parameter ::= MISSING {: RESULT = new StepNoValue(); :} | REDEFINE {: RESULT = new StepRedefinedValue(); :} | INTEGER:i {: RESULT = new StepIntegerValue(i); :} | REAL:r {: RESULT = new StepRealValue(r); :} | LOGICAL_TRUE {: RESULT = new StepLogicalValue(true); :} | LOGICAL_FALSE {: RESULT = new StepLogicalValue(false); :} | LOGICAL_UNKNOWN {: RESULT = new StepLogicalValue(); :} | ENUMERATION:e {: RESULT = new StepEnumerationValue(e); :} | STRING:s {: RESULT = new StepStringValue(s); :} | ENCODED_STRING:s {: RESULT = new StepEncodedStringValue(s); :} | BINARY:s {: RESULT = new StepBinaryValue(s); :} | TypedValue:t {: RESULT = t; :} | InstanceReference:i {: RESULT = i; :} | EmbeddedList:l {: RESULT = l; :} ; InstanceReference ::= InstRef:r {: RESULT = r; :} ; ScopeInstanceReference ::= InstRef:r {: RESULT = r; :} ; Entity_export ::= /* NULL */ {:/* $$ = 0; */:} | SLASH ScopeInstRefs SLASH {:/* $$ = $2; */:} ; ScopeInstRefs ::= ScopeInstanceReference {:/* if (nopass == 2) { $$ = new set >; if ($1) { $$->insert($1); } } */:} | ScopeInstRefs COMMA ScopeInstanceReference {:/* if (nopass == 2) { if ($3) { $1->insert($3); } } */:} ; EmbeddedList ::= LEFT_PAREN List:l RIGHT_PAREN {: RESULT = l; :} | LEFT_PAREN RIGHT_PAREN {: RESULT = new StepAggregationValue(); :} ; List ::= ListEntry:e {: RESULT = new StepAggregationValue(); ((StepAggregationValue)RESULT).add(e); :} | List:l COMMA ListEntry:e {: RESULT = l; ((StepAggregationValue)RESULT).add(e); :} ; ListEntry ::= Parameter:p {: RESULT = p; :} ;