diff options
author | Kristóf Marussy <marussy@mit.bme.hu> | 2020-06-23 15:17:00 +0200 |
---|---|---|
committer | Kristóf Marussy <marussy@mit.bme.hu> | 2020-06-23 15:17:00 +0200 |
commit | 42c58bbeead1dae09c51f47abc8e10dfbb9c3f9f (patch) | |
tree | 21d49c3a1dadc54dda399a4b8cfe8dc028563f51 /Application/org.eclipse.viatra.solver.language/src/org | |
parent | Measurements framework fixes (diff) | |
download | VIATRA-Generator-42c58bbeead1dae09c51f47abc8e10dfbb9c3f9f.tar.gz VIATRA-Generator-42c58bbeead1dae09c51f47abc8e10dfbb9c3f9f.tar.zst VIATRA-Generator-42c58bbeead1dae09c51f47abc8e10dfbb9c3f9f.zip |
New configuration language parser WIP
Diffstat (limited to 'Application/org.eclipse.viatra.solver.language/src/org')
3 files changed, 156 insertions, 108 deletions
diff --git a/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/GenerateSolverLanguage.mwe2 b/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/GenerateSolverLanguage.mwe2 index 50072f0d..644e681d 100644 --- a/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/GenerateSolverLanguage.mwe2 +++ b/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/GenerateSolverLanguage.mwe2 | |||
@@ -32,7 +32,7 @@ Workflow { | |||
32 | language = StandardLanguage { | 32 | language = StandardLanguage { |
33 | name = "org.eclipse.viatra.solver.language.SolverLanguage" | 33 | name = "org.eclipse.viatra.solver.language.SolverLanguage" |
34 | fileExtensions = "vsc" | 34 | fileExtensions = "vsc" |
35 | referencedResource = "platform:/resource/org.eclipse.viatra.solver.language/model/SolverLanguage.genmodel" | 35 | referencedResource = "platform:/resource/org.eclipse.viatra.solver.language.model/model/model.genmodel" |
36 | 36 | ||
37 | serializer = { | 37 | serializer = { |
38 | generateStub = false | 38 | generateStub = false |
diff --git a/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/SolverLanguage.xtext b/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/SolverLanguage.xtext index d0578d78..51115da0 100644 --- a/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/SolverLanguage.xtext +++ b/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/SolverLanguage.xtext | |||
@@ -3,66 +3,80 @@ import "http://www.eclipse.org/emf/2002/Ecore" as ecore | |||
3 | import "http://www.eclipse.org/viatra/solver/language/SolverLanguage" | 3 | import "http://www.eclipse.org/viatra/solver/language/SolverLanguage" |
4 | 4 | ||
5 | Problem: | 5 | Problem: |
6 | (statements+=Statement)*; | 6 | ("problem" name=QualifiedName FULL_STOP)? |
7 | imports+=Import* | ||
8 | statements+=Statement*; | ||
7 | 9 | ||
8 | Statement: | 10 | Statement: |
9 | ( | 11 | ( |
10 | AssertionOrDefinition | PredicateDefinition | UnnamedErrorPrediateDefinition | DefaultDefinition | MetricDefinition | | 12 | AssertionOrDefinition | PredicateDefinition | UnnamedErrorPredicateDefintion | DefaultAssertion | |
11 | ExternPredicateDefinition | ExternMetricDefinition | ExternAggregatorDefinition | ExternDatatypeDefinition | | 13 | FunctionDefinition | Attribute | ExternDeclaration | ScopeDeclaration | ObjectiveDeclaration | |
12 | ClassDefinition | ScopeDefinition | ObjectiveDefinition | 14 | ClassDeclaration | EnumDeclaration |
13 | ) FULL_STOP; | 15 | ); |
16 | |||
17 | Import: | ||
18 | UriImport | NamespaceImport; | ||
19 | |||
20 | UriImport: | ||
21 | "import" uri=STRING ("as" alias=QualifiedName) FULL_STOP; | ||
22 | |||
23 | NamespaceImport: | ||
24 | "import" importedNamespace=QualifiedName ("as" alias=QualifiedName) FULL_STOP; | ||
14 | 25 | ||
15 | AssertionOrDefinition returns Statement: | 26 | AssertionOrDefinition returns Statement: |
16 | Expression ( | 27 | Expression ( |
17 | {Interpretation.body=current} ":" range=Expression | | 28 | {Assertion.expression=current} (":" range=Expression)? | |
18 | {PredicateDefinition.head=current} ":-" body=Expression | | 29 | {PredicateDefinition.head=current} ":-" body=Expression | |
19 | {MetricDefinition.head=current} "=" body=Expression | 30 | {FunctionDefinition.head=current} ":=" body=Expression |
20 | )?; | 31 | ) FULL_STOP; |
21 | 32 | ||
22 | PredicateDefinition: | 33 | PredicateDefinition: |
23 | (functional?="functional" error?="error"? | error?="error" functional?="functional"?) head=Call ":-" body=Expression; | 34 | (functional?="functional" error?="error"? | error?="error" functional?="functional"?) head=Call ":-" body=Expression FULL_STOP; |
24 | 35 | ||
25 | UnnamedErrorPrediateDefinition: | 36 | UnnamedErrorPredicateDefintion: |
26 | "error" argumentList=ArgumentList ":-" body=Expression; | 37 | "error" argumentList=ArgumentList ":-" body=Expression FULL_STOP; |
27 | 38 | ||
28 | DefaultDefinition: | 39 | DefaultAssertion: |
29 | "default" head=Call ":" range=Expression; | 40 | "default" expression=Call (":" range=Expression)? FULL_STOP; |
30 | 41 | ||
31 | MetricDefinition: | 42 | FunctionDefinition: |
32 | type=[NamedElement|QualifiedName] head=Call "=" body=Expression; | 43 | resultType=[Symbol|QualifiedName] head=Call ":=" body=Expression FULL_STOP; |
33 | 44 | ||
34 | ExternPredicateDefinition: | 45 | TypeReference: |
35 | "extern" name=QualifiedName argumentList=ArgumentList; | 46 | type=[Symbol|QualifiedName] forceObjectType?="object"?; |
36 | 47 | ||
37 | ExternMetricDefinition: | 48 | enum AttributeKind: |
38 | "extern" type=[NamedElement|QualifiedName] name=QualifiedName argumentList=ArgumentList; | 49 | FUNCTIONAL="functional" | ERROR="error" | ROOT="root" | CONTAINMENT="containment"; |
39 | 50 | ||
40 | ExternAggregatorDefinition: | 51 | Attribute: |
41 | "extern" type=[NamedElement|QualifiedName] name=QualifiedName "{" inputType=[NamedElement|QualifiedName] "..." "}"; | 52 | kind=AttributeKind target=[Symbol|QualifiedName] FULL_STOP; |
42 | 53 | ||
43 | ExternDatatypeDefinition: | 54 | ExternDeclaration: |
44 | "extern" "datatype" name=QualifiedName ("extends" supertypes+=[NamedElement|QualifiedName] ("," supertypes+=[NamedElement|QualifiedName])*); | 55 | ExternPredicateDeclaration | ExternFunctionDeclaration | ExternAggregationOperatorDeclaration | ExternDatatypeDeclaration; |
45 | 56 | ||
46 | Variable: | 57 | ExternPredicateDeclaration: |
47 | type=[NamedElement|QualifiedName]? name=ID; | 58 | "extern" (functional?="functional"? & error?="error"?) name=QualifiedName argumentList=ArgumentList FULL_STOP; |
59 | |||
60 | ExternFunctionDeclaration: | ||
61 | "extern" resultType=[Symbol|QualifiedName] name=QualifiedName argumentList=ArgumentList FULL_STOP; | ||
62 | |||
63 | ExternAggregationOperatorDeclaration: | ||
64 | "extern" resultType=[Symbol|QualifiedName] name=QualifiedName "{" argumentType=[Symbol|QualifiedName] "..." "}" FULL_STOP; | ||
65 | |||
66 | ExternDatatypeDeclaration: | ||
67 | "extern" "datatype" name=QualifiedName FULL_STOP; | ||
48 | 68 | ||
49 | Expression: | 69 | Expression: |
50 | IfElse | Let | ImplicationExpression; | 70 | ConditionalExpression | LetExpression | DisjunctiveExpression ({Forall.condition=current} "=>" body=DisjunctiveExpression)?; |
51 | 71 | ||
52 | IfElse: | 72 | ConditionalExpression: |
53 | "if" condition=Expression "then" then=Expression "else" else=Expression; | 73 | "if" condition=DisjunctiveExpression "then" then=Expression "else" else=Expression; |
54 | 74 | ||
55 | Let: | 75 | LetExpression: |
56 | "let" bindings+=LetBinding ("," bindings+=LetBinding)* "in" body=Expression; | 76 | "let" bindings+=LetBinding ("," bindings+=LetBinding)* "in" body=Expression; |
57 | 77 | ||
58 | LetBinding: | 78 | LetBinding: |
59 | variable=Variable "=" value=AdditiveExpression; | 79 | type=[Symbol|QualifiedName]? name=ID "=" value=AdditiveExpression; |
60 | |||
61 | enum ImplicationOperator returns BinaryOperator: | ||
62 | IMPLIES = "=>"; | ||
63 | |||
64 | ImplicationExpression returns Expression: | ||
65 | DisjunctiveExpression ({BinaryExpression.left=current} op=ImplicationOperator right=ImplicationExpression)?; | ||
66 | 80 | ||
67 | DisjunctiveExpression returns Expression: | 81 | DisjunctiveExpression returns Expression: |
68 | ConjunctiveExpression ( | 82 | ConjunctiveExpression ( |
@@ -77,19 +91,19 @@ ConjunctiveExpression returns Expression: | |||
77 | ComparisonExpression ({Conjunction.children+=current} ("," children+=ComparisonExpression)+)?; | 91 | ComparisonExpression ({Conjunction.children+=current} ("," children+=ComparisonExpression)+)?; |
78 | 92 | ||
79 | enum ComparisonOperator returns BinaryOperator: | 93 | enum ComparisonOperator returns BinaryOperator: |
80 | EQ="==" | NOT_EQ="!=" | LESS="<" | LESS_EQ="<=" | GREATER=">" | GREATER_EQ=">=" | IN="in"; | 94 | EQUALS="==" | NOT_EQUALS="!=" | LESS="<" | LESS_EQUALS="<=" | GREATER=">" | GREATER_EQUALS=">=" | IN="in"; |
81 | 95 | ||
82 | ComparisonExpression returns Expression: | 96 | ComparisonExpression returns Expression: |
83 | AdditiveExpression ({Comparison.left=current} op=ComparisonOperator right=AdditiveExpression)?; | 97 | AdditiveExpression ({BinaryExpression.left=current} op=ComparisonOperator right=AdditiveExpression)?; |
84 | 98 | ||
85 | enum AdditiveBinaryOperator returns BinaryOperator: | 99 | enum AdditiveBinaryOperator returns BinaryOperator: |
86 | ADD="+" | SUB="-"; | 100 | PLUS="+" | MINUS="-"; |
87 | 101 | ||
88 | AdditiveExpression returns Expression: | 102 | AdditiveExpression returns Expression: |
89 | MultiplicativeExpression ({BinaryExpression.left=current} op=AdditiveBinaryOperator right=MultiplicativeExpression)*; | 103 | MultiplicativeExpression ({BinaryExpression.left=current} op=AdditiveBinaryOperator right=MultiplicativeExpression)*; |
90 | 104 | ||
91 | enum MultiplicativeBinaryOperator returns BinaryOperator: | 105 | enum MultiplicativeBinaryOperator returns BinaryOperator: |
92 | MUL="*" | DIV="/"; | 106 | MULTIPLY="*" | DIVIDE="/"; |
93 | 107 | ||
94 | MultiplicativeExpression returns Expression: | 108 | MultiplicativeExpression returns Expression: |
95 | ExponentialExpression ({BinaryExpression.left=current} op=MultiplicativeBinaryOperator right=ExponentialExpression)*; | 109 | ExponentialExpression ({BinaryExpression.left=current} op=MultiplicativeBinaryOperator right=ExponentialExpression)*; |
@@ -101,61 +115,59 @@ ExponentialExpression returns Expression: | |||
101 | CastExpression ({BinaryExpression.left=current} op=ExponentialOp right=CastExpression)?; | 115 | CastExpression ({BinaryExpression.left=current} op=ExponentialOp right=CastExpression)?; |
102 | 116 | ||
103 | CastExpression returns Expression: | 117 | CastExpression returns Expression: |
104 | UnaryExpression ({CastExpression.body=current} "as" type=[NamedElement|QualifiedName])?; | 118 | UnaryExpression ({CastExpression.body=current} "as" targetType=[Symbol|QualifiedName])?; |
105 | 119 | ||
106 | enum UnaryOp: | 120 | enum UnaryOperator: |
107 | NEG="!" | PLUS="+" | MINUS="-" | MAY="may" | MUST="must" | CURRENT="current"; | 121 | NOT="!" | PLUS="+" | MINUS="-" | MAYBE="?"; |
108 | 122 | ||
109 | UnaryExpression returns Expression: | 123 | UnaryExpression returns Expression: |
110 | AggregationExpression | {UnaryExpression} op=UnaryOp body=AggregationExpression; | 124 | BracedAggregateExpression | {UnaryExpression} op=UnaryOperator body=BracedAggregateExpression; |
111 | |||
112 | AggregationExpression returns Expression: | ||
113 | AtomicExpression | QuantifiedExpression | Aggregation; | ||
114 | 125 | ||
115 | LocalVariables: | 126 | BracedAggregateExpression returns Expression: |
116 | {LocalVariables} "[" (variables+=Variable ("," variables+=Variable)*)? "]"; | 127 | AtomicExpression | Aggregation | Count ; |
117 | |||
118 | enum Quantifier: | ||
119 | EXISTS="exists" | FORALL="forall" | COUNT="count"; | ||
120 | |||
121 | QuantifiedExpression: | ||
122 | quantifier=Quantifier localVariables=LocalVariables? "{" body=Expression "}"; | ||
123 | 128 | ||
124 | Aggregation: | 129 | Aggregation: |
125 | op=[NamedElement|QualifiedName] localVariables=LocalVariables? "{" body=Expression ("|" condition=Expression)? "}"; | 130 | op=[Symbol|QualifiedName] "{" value=Expression "|" condition=Expression "}"; |
131 | |||
132 | Count: | ||
133 | "count" "{" condition=Expression "}"; | ||
126 | 134 | ||
127 | AtomicExpression returns Expression: | 135 | AtomicExpression returns Expression: |
128 | Reference | Call | Interval | Literal | "(" Expression ")"; | 136 | Reference | Call | Interval | Literal | "(" Expression ")"; |
129 | 137 | ||
130 | Call: | 138 | Call: |
131 | functor=Reference (transitiveClosure?=TRANSITIVE_CLOSURE | reflexiveTransitiveClosure?=REFLEXIVE_TRANSITIVE_CLOSURE)? argumentList=ArgumentList; | 139 | functor=Reference argumentList=ArgumentList; |
132 | 140 | ||
133 | ArgumentList: | 141 | ArgumentList: |
134 | {ArgumentList} "(" (arguments+=Argument ("," arguments+=Argument)*)? ")"; | 142 | {ArgumentList} "(" (arguments+=Argument ("," arguments+=Argument)*)? ")"; |
135 | 143 | ||
136 | Argument: | 144 | Argument: |
137 | ExpressionArgument | StarArgument | TypedArgument | TypedStarArgument; | 145 | ExpressionArgument | StarArgument | TypedVariableArgument | TypedStarArgument; |
138 | 146 | ||
139 | ExpressionArgument: | 147 | ExpressionArgument: |
140 | body=ComparisonExpression; | 148 | expression=ComparisonExpression; |
141 | 149 | ||
142 | StarArgument: | 150 | StarArgument: |
143 | {StarArgument} "*"; | 151 | {StarArgument} "*"; |
144 | 152 | ||
145 | TypedArgument: | 153 | TypedVariableArgument: |
146 | type=[NamedElement|QualifiedName] variable=[NamedElement|QualifiedName]; | 154 | typeReference=TypeReference name=ID; |
147 | 155 | ||
148 | TypedStarArgument: | 156 | TypedStarArgument: |
149 | type=[NamedElement|QualifiedName] "*"; | 157 | typeReference=TypeReference "*"; |
150 | 158 | ||
151 | Reference: | 159 | Reference: |
152 | referred=[NamedElement|QualifiedName]; | 160 | components+=PathComponent ("." components+=PathComponent)*; |
161 | |||
162 | PathComponent: | ||
163 | inverse?="~"? symbol=[Symbol|QualifiedName] | ||
164 | (transitiveClosure?=TRANSITIVE_CLOSURE | reflexiveTransitiveClosure?=REFLEXIVE_TRANSITIVE_CLOSURE)?; | ||
153 | 165 | ||
154 | Interval: | 166 | Interval: |
155 | "[" lowerBound=Expression ".." upperBound=Expression "]"; | 167 | "[" lowerBound=AdditiveExpression "," upperBound=AdditiveExpression "]"; |
156 | 168 | ||
157 | Literal: | 169 | Literal: |
158 | LogicLiteral | NumericLiteral | InfinityLiteral | EmptyIntervalLiteral | StringLiteral; | 170 | LogicLiteral | IntLiteral | RealLiteral | InfinityLiteral | EmptyLiteral | StringLiteral; |
159 | 171 | ||
160 | enum LogicValue: | 172 | enum LogicValue: |
161 | TRUE="true" | FALSE="false" | UNKNOWN="unknown" | ERROR="error"; | 173 | TRUE="true" | FALSE="false" | UNKNOWN="unknown" | ERROR="error"; |
@@ -163,69 +175,82 @@ enum LogicValue: | |||
163 | LogicLiteral: | 175 | LogicLiteral: |
164 | value=LogicValue; | 176 | value=LogicValue; |
165 | 177 | ||
166 | NumericLiteral: | 178 | IntLiteral: |
179 | value=INT; | ||
180 | |||
181 | RealLiteral: | ||
167 | value=Real; | 182 | value=Real; |
168 | 183 | ||
169 | InfinityLiteral: | 184 | InfinityLiteral: |
170 | {InfinityLiteral} "inf"; | 185 | {InfinityLiteral} "inf"; |
171 | 186 | ||
172 | EmptyIntervalLiteral: | 187 | EmptyLiteral: |
173 | {EmptyIntervalLiteral} "empty"; | 188 | {EmptyLiteral} "empty"; |
174 | 189 | ||
175 | StringLiteral: | 190 | StringLiteral: |
176 | value=STRING; | 191 | value=STRING; |
177 | 192 | ||
178 | ClassDefinition: | 193 | ClassDeclaration: |
179 | abstract?="abstract"? "class" name=ID ("extends" superclasses+=[NamedElement|QualifiedName] ("," superclasses+=[NamedElement|QualifiedName])*)? | 194 | (abstract?="abstract"? & root?="root"?) "class" name=ID ("extends" supertypes+=[Symbol|QualifiedName] ("," supertypes+=[Symbol|QualifiedName])*)? |
180 | "{" members+=MemberDefinition* "}"; | 195 | ("{" fields+=Field* "}" | FULL_STOP); |
181 | 196 | ||
182 | MemberDefinition: | 197 | Field: |
183 | containment?="contains"? type=[NamedElement|QualifiedName] multiplicity=Multiplicity? name=ID ("opposite" opposite=[NamedElement|QualifiedName])? ";"?; | 198 | (containment?="contains" | crossReference?="refers")? type=[Symbol|QualifiedName] multiplicity=Multiplicity? |
199 | name=ID ("opposite" opposite=[Symbol|QualifiedName])? ";"?; | ||
184 | 200 | ||
185 | Multiplicity: | 201 | Multiplicity: |
186 | ManyMultiplicity | ExactMultiplicity | BoundedMultiplicity; | 202 | UnboundedMultiplicity | ExactMultiplicity | BoundedMultiplicity; |
187 | 203 | ||
188 | ManyMultiplicity: | 204 | UnboundedMultiplicity: |
189 | {ManyMultiplicity} "[" "]"; | 205 | {UnboundedMultiplicity} "[" "]"; |
190 | 206 | ||
191 | ExactMultiplicity: | 207 | ExactMultiplicity: |
192 | "[" multiplicity=UpperMultiplicty "]"; | 208 | "[" value=UpperMultiplicty "]"; |
193 | 209 | ||
194 | BoundedMultiplicity: | 210 | BoundedMultiplicity: |
195 | "[" lowerBound=INT ".." upperBound=UpperMultiplicty "]"; | 211 | "[" lowerBound=INT "," upperBound=UpperMultiplicty "]"; |
212 | |||
213 | EnumDeclaration: | ||
214 | "enum" name=ID ("{" (literals+=EnumLiteral (","? literals+=EnumLiteral)*)? "}" | FULL_STOP); | ||
196 | 215 | ||
197 | ScopeDefinition: | 216 | EnumLiteral: |
198 | ExactScopeDefinition | BoundedScopeDefinition | LowerBoundedScopeDefinition; | 217 | name=ID; |
199 | 218 | ||
200 | ExactScopeDefinition: | 219 | ScopeDeclaration: |
201 | "scope" type=[NamedElement|QualifiedName] "==" exactScope=INT; | 220 | ExactScope | BoundedScope | LowerBoundedScope; |
202 | 221 | ||
203 | BoundedScopeDefinition: | 222 | ExactScope: |
223 | "scope" type=[Symbol|QualifiedName] "==" size=INT FULL_STOP; | ||
224 | |||
225 | BoundedScope: | ||
204 | "scope" ( | 226 | "scope" ( |
205 | (lowerBound=INT "<=")? type=[NamedElement|QualifiedName] "<=" upperBound=INT | | 227 | (lowerBound=INT "<=")? type=[Symbol|QualifiedName] "<=" upperBound=INT | |
206 | upperBound=INT ">=" type=[NamedElement|QualifiedName] (">=" lowerBound=INT)? | 228 | upperBound=INT ">=" type=[Symbol|QualifiedName] (">=" lowerBound=INT)? |
207 | ); | 229 | ) FULL_STOP; |
208 | 230 | ||
209 | LowerBoundedScopeDefinition: | 231 | LowerBoundedScope: |
210 | "scope" ( | 232 | "scope" ( |
211 | lowerBound=INT "<=" type=[NamedElement|QualifiedName] | | 233 | lowerBound=INT "<=" type=[Symbol|QualifiedName] | |
212 | type=[NamedElement|QualifiedName] ">=" lowerBound=INT | 234 | type=[Symbol|QualifiedName] ">=" lowerBound=INT |
213 | ); | 235 | ) FULL_STOP; |
214 | 236 | ||
215 | enum ObjectiveKind: | 237 | enum ObjectiveKind: |
216 | MINIMIZE="minimize" | MAXIMIZE="maximize"; | 238 | MINIMIZATION="minimize" | MAXIMIZATION="maximize"; |
217 | 239 | ||
218 | ObjectiveDefinition: | 240 | ObjectiveDeclaration: |
219 | kind=ObjectiveKind objective=Expression; | 241 | kind=ObjectiveKind objective=Expression FULL_STOP; |
220 | 242 | ||
221 | UpperMultiplicty returns ecore::EInt: | 243 | UpperMultiplicty returns ecore::EInt: |
222 | INT | "*"; | 244 | INT | "*"; |
223 | 245 | ||
224 | Real returns ecore::EBigDecimal hidden(): | 246 | Real returns ecore::EBigDecimal hidden(): |
225 | INT ("." INT)?; | 247 | INT "." INT; |
248 | |||
249 | QualifiedNameSegment: | ||
250 | ID | QUOTED_ID | "object"; | ||
226 | 251 | ||
227 | QualifiedName hidden(): | 252 | QualifiedName hidden(): |
228 | ID ("." ID)* | QUOTED_ID; | 253 | QualifiedNameSegment ("::" QualifiedNameSegment)*; |
229 | 254 | ||
230 | @Override | 255 | @Override |
231 | terminal STRING returns ecore::EString: | 256 | terminal STRING returns ecore::EString: |
diff --git a/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/parser/antlr/SolverLanguageTokenSource.java b/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/parser/antlr/SolverLanguageTokenSource.java index 4c1dacd5..70e7e492 100644 --- a/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/parser/antlr/SolverLanguageTokenSource.java +++ b/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/parser/antlr/SolverLanguageTokenSource.java | |||
@@ -29,6 +29,7 @@ public class SolverLanguageTokenSource implements TokenSource { | |||
29 | private int plusTokenId = -1; | 29 | private int plusTokenId = -1; |
30 | private int starTokenId = -1; | 30 | private int starTokenId = -1; |
31 | private int dotTokenId = -1; | 31 | private int dotTokenId = -1; |
32 | private int tildeTokenId = -1; | ||
32 | private int openParenTokenId = -1; | 33 | private int openParenTokenId = -1; |
33 | 34 | ||
34 | public SolverLanguageTokenSource(TokenSource delegate) { | 35 | public SolverLanguageTokenSource(TokenSource delegate) { |
@@ -42,33 +43,49 @@ public class SolverLanguageTokenSource implements TokenSource { | |||
42 | 43 | ||
43 | @Override | 44 | @Override |
44 | public Token nextToken() { | 45 | public Token nextToken() { |
45 | Token token = internalNextToken(); | 46 | Token token; |
47 | if (acceptor.hasNext()) { | ||
48 | token = acceptor.next(); | ||
49 | } else { | ||
50 | token = delegate.nextToken(); | ||
51 | } | ||
52 | postProcessToken(token); | ||
46 | lastTokenId = token.getType(); | 53 | lastTokenId = token.getType(); |
47 | return token; | 54 | return token; |
48 | } | 55 | } |
49 | 56 | ||
50 | protected Token internalNextToken() { | 57 | protected void postProcessToken(Token token) { |
51 | if (acceptor.hasNext()) { | ||
52 | return acceptor.next(); | ||
53 | } | ||
54 | Token token = delegate.nextToken(); | ||
55 | int type = token.getType(); | 58 | int type = token.getType(); |
56 | if (type == plusTokenId) { | 59 | if (type == plusTokenId) { |
57 | if ((lastTokenId == RULE_ID || lastTokenId == RULE_QUOTED_ID) | 60 | if (isTransitiveClosureInReferenceContext()) { |
58 | && peekUntilVisible().getType() == openParenTokenId) { | ||
59 | token.setType(RULE_TRANSITIVE_CLOSURE); | 61 | token.setType(RULE_TRANSITIVE_CLOSURE); |
60 | } | 62 | } |
61 | } else if (type == starTokenId) { | 63 | } else if (type == starTokenId) { |
62 | if ((lastTokenId == RULE_ID || lastTokenId == RULE_QUOTED_ID) | 64 | if (isTransitiveClosureInReferenceContext()) { |
63 | && peekUntilVisible().getType() == openParenTokenId) { | ||
64 | token.setType(RULE_REFLEXIVE_TRANSITIVE_CLOSURE); | 65 | token.setType(RULE_REFLEXIVE_TRANSITIVE_CLOSURE); |
65 | } | 66 | } |
66 | } else if (type == dotTokenId) { | 67 | } else if (type == dotTokenId) { |
67 | if ((lastTokenId != RULE_ID && lastTokenId != RULE_INT) || peekToken().getType() != lastTokenId) { | 68 | if (isFullStopContext()) { |
68 | token.setType(RULE_FULL_STOP); | 69 | token.setType(RULE_FULL_STOP); |
69 | } | 70 | } |
70 | } | 71 | } |
71 | return token; | 72 | } |
73 | |||
74 | private boolean isIdFragment(int tokenId) { | ||
75 | return tokenId == RULE_ID || tokenId == RULE_QUOTED_ID; | ||
76 | } | ||
77 | |||
78 | private boolean isTransitiveClosureInReferenceContext() { | ||
79 | int nextVisibleTokenId = peekUntilVisible().getType(); | ||
80 | return isIdFragment(lastTokenId) | ||
81 | && (nextVisibleTokenId == dotTokenId || nextVisibleTokenId == openParenTokenId); | ||
82 | } | ||
83 | |||
84 | private boolean isFullStopContext() { | ||
85 | int nextTokenId = peekToken().getType(); | ||
86 | boolean inReference = isIdFragment(lastTokenId) && (isIdFragment(nextTokenId) || nextTokenId == tildeTokenId); | ||
87 | boolean inReal = lastTokenId == RULE_INT && nextTokenId == RULE_INT; | ||
88 | return !(inReference || inReal); | ||
72 | } | 89 | } |
73 | 90 | ||
74 | protected Token peekUntilVisible() { | 91 | protected Token peekUntilVisible() { |
@@ -101,6 +118,9 @@ public class SolverLanguageTokenSource implements TokenSource { | |||
101 | case "'.'": | 118 | case "'.'": |
102 | dotTokenId = entry.getKey(); | 119 | dotTokenId = entry.getKey(); |
103 | break; | 120 | break; |
121 | case "'~'": | ||
122 | tildeTokenId = entry.getKey(); | ||
123 | break; | ||
104 | case "'('": | 124 | case "'('": |
105 | openParenTokenId = entry.getKey(); | 125 | openParenTokenId = entry.getKey(); |
106 | break; | 126 | break; |
@@ -115,6 +135,9 @@ public class SolverLanguageTokenSource implements TokenSource { | |||
115 | if (dotTokenId == -1) { | 135 | if (dotTokenId == -1) { |
116 | throw new IllegalStateException("Token '.' was not found"); | 136 | throw new IllegalStateException("Token '.' was not found"); |
117 | } | 137 | } |
138 | if (tildeTokenId == -1) { | ||
139 | throw new IllegalStateException("Token '~' was not found"); | ||
140 | } | ||
118 | if (openParenTokenId == -1) { | 141 | if (openParenTokenId == -1) { |
119 | throw new IllegalStateException("Token '(' was not found"); | 142 | throw new IllegalStateException("Token '(' was not found"); |
120 | } | 143 | } |