diff options
author | 2020-05-08 21:02:33 +0200 | |
---|---|---|
committer | 2020-05-08 21:02:33 +0200 | |
commit | 771e3773bb7ebbf6712724cb2b248467c9cc2e3c (patch) | |
tree | b1724af24b9c583b2780c7a2fe76e76fd174359d /Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra | |
parent | Update solver language grammar (diff) | |
download | VIATRA-Generator-771e3773bb7ebbf6712724cb2b248467c9cc2e3c.tar.gz VIATRA-Generator-771e3773bb7ebbf6712724cb2b248467c9cc2e3c.tar.zst VIATRA-Generator-771e3773bb7ebbf6712724cb2b248467c9cc2e3c.zip |
Synthetic tokens for solver language
Diffstat (limited to 'Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra')
5 files changed, 189 insertions, 14 deletions
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 5abeb891..1306b626 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 | |||
@@ -9,7 +9,7 @@ Statement: | |||
9 | ( | 9 | ( |
10 | AssertionOrDefinition | PredicateDefinition | UnnamedErrorPrediateDefinition | DefaultDefinition | ExternPredicateDefinition | | 10 | AssertionOrDefinition | PredicateDefinition | UnnamedErrorPrediateDefinition | DefaultDefinition | ExternPredicateDefinition | |
11 | MetricDefinition | ExternMetricDefinition | ClassDefinition | ScopeDefinition | ObjectiveDefinition | 11 | MetricDefinition | ExternMetricDefinition | ClassDefinition | ScopeDefinition | ObjectiveDefinition |
12 | ) DOT; | 12 | ) FULL_STOP; |
13 | 13 | ||
14 | AssertionOrDefinition returns Statement: | 14 | AssertionOrDefinition returns Statement: |
15 | Expression ( | 15 | Expression ( |
@@ -28,7 +28,7 @@ DefaultDefinition: | |||
28 | "default" head=Call ":" range=Expression; | 28 | "default" head=Call ":" range=Expression; |
29 | 29 | ||
30 | ExternPredicateDefinition: | 30 | ExternPredicateDefinition: |
31 | "extern" head=Call "."; | 31 | "extern" head=Call; |
32 | 32 | ||
33 | enum MetricType: | 33 | enum MetricType: |
34 | INT="int" | REAL="real"; | 34 | INT="int" | REAL="real"; |
@@ -103,10 +103,10 @@ Aggregation: | |||
103 | op=AggregationOp "{" body=Expression "|" condition=Expression "}"; | 103 | op=AggregationOp "{" body=Expression "|" condition=Expression "}"; |
104 | 104 | ||
105 | AtomicExpression returns Expression: | 105 | AtomicExpression returns Expression: |
106 | Reference ({Call.functor=current} -> argumentList=ArgumentList)? | Interval | Literal | "(" Expression ")"; | 106 | Reference | Call | Interval | Literal | "(" Expression ")"; |
107 | 107 | ||
108 | Call: | 108 | Call: |
109 | functor=Reference (transitiveClosure?=STAR | reflexiveTransitiveClosure?=PLUS)? argumentList=ArgumentList; | 109 | functor=Reference (transitiveClosure?=TRANSITIVE_CLOSURE | reflexiveTransitiveClosure?=REFLEXIVE_TRANSITIVE_CLOSURE)? argumentList=ArgumentList; |
110 | 110 | ||
111 | ArgumentList: | 111 | ArgumentList: |
112 | {ArgumentList} "(" (arguments+=Argument ("," arguments+=Argument)*)? ")"; | 112 | {ArgumentList} "(" (arguments+=Argument ("," arguments+=Argument)*)? ")"; |
@@ -153,7 +153,7 @@ EmptyIntervalLiteral: | |||
153 | StringLiteral: | 153 | StringLiteral: |
154 | value=STRING; | 154 | value=STRING; |
155 | 155 | ||
156 | ClassDefinition returns Statement: | 156 | ClassDefinition: |
157 | abstract?="abstract"? "class" name=ID ("extends" superclasses+=[NamedElement|QualifiedName] ("," superclasses+=[NamedElement|QualifiedName])*)? | 157 | abstract?="abstract"? "class" name=ID ("extends" superclasses+=[NamedElement|QualifiedName] ("," superclasses+=[NamedElement|QualifiedName])*)? |
158 | "{" members+=MemberDefinition* "}"; | 158 | "{" members+=MemberDefinition* "}"; |
159 | 159 | ||
@@ -182,13 +182,13 @@ BoundedScopeDefinition: | |||
182 | "scope" ( | 182 | "scope" ( |
183 | (lowerBound=INT "<=")? type=[NamedElement|QualifiedName] "<=" upperBound=INT | | 183 | (lowerBound=INT "<=")? type=[NamedElement|QualifiedName] "<=" upperBound=INT | |
184 | upperBound=INT ">=" type=[NamedElement|QualifiedName] (">=" lowerBound=INT)? | 184 | upperBound=INT ">=" type=[NamedElement|QualifiedName] (">=" lowerBound=INT)? |
185 | ) "."; | 185 | ); |
186 | 186 | ||
187 | LowerBoundedScopeDefinition: | 187 | LowerBoundedScopeDefinition: |
188 | "scope" ( | 188 | "scope" ( |
189 | lowerBound=INT "<=" type=[NamedElement|QualifiedName] | | 189 | lowerBound=INT "<=" type=[NamedElement|QualifiedName] | |
190 | type=[NamedElement|QualifiedName] ">=" lowerBound=INT | 190 | type=[NamedElement|QualifiedName] ">=" lowerBound=INT |
191 | ) "."; | 191 | ); |
192 | 192 | ||
193 | enum ObjectiveKind: | 193 | enum ObjectiveKind: |
194 | MINIMIZE="minimize" | MAXIMIZE="maximize"; | 194 | MINIMIZE="minimize" | MAXIMIZE="maximize"; |
@@ -209,17 +209,21 @@ QualifiedName hidden(): | |||
209 | terminal STRING returns ecore::EString: | 209 | terminal STRING returns ecore::EString: |
210 | '"' ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|'"') )* '"'; | 210 | '"' ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|'"') )* '"'; |
211 | 211 | ||
212 | terminal QUOTED_ID returns ecore::EString: | 212 | terminal QUOTED_ID: |
213 | '\'' ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|'\'') )* '\''; | 213 | '\'' ( '\\' . /* 'b'|'t'|'n'|'f'|'r'|'u'|'"'|"'"|'\\' */ | !('\\'|'\'') )* '\''; |
214 | 214 | ||
215 | terminal PLUS: | 215 | @Override |
216 | "synthetic::plus"; | 216 | terminal SL_COMMENT: |
217 | ('%' | '//') !('\n'|'\r')* ('\r'? '\n')?; | ||
218 | |||
219 | terminal TRANSITIVE_CLOSURE: | ||
220 | "synthetic:TRANSITIVE_CLOSURE"; | ||
217 | 221 | ||
218 | terminal STAR: | 222 | terminal REFLEXIVE_TRANSITIVE_CLOSURE: |
219 | "synthetic::star"; | 223 | "synthetic:REFLEXIVE_TRANSITIVE_CLOSURE"; |
220 | 224 | ||
221 | terminal DOT: | 225 | terminal FULL_STOP: |
222 | "synthetic::dot"; | 226 | "synthetic:FULL_STOP"; |
223 | 227 | ||
224 | NamedElement: | 228 | NamedElement: |
225 | name=QualifiedName; | 229 | name=QualifiedName; |
diff --git a/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/SolverLanguageRuntimeModule.xtend b/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/SolverLanguageRuntimeModule.xtend index 343bc48a..e882626b 100644 --- a/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/SolverLanguageRuntimeModule.xtend +++ b/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/SolverLanguageRuntimeModule.xtend | |||
@@ -3,9 +3,21 @@ | |||
3 | */ | 3 | */ |
4 | package org.eclipse.viatra.solver.language | 4 | package org.eclipse.viatra.solver.language |
5 | 5 | ||
6 | import org.eclipse.viatra.solver.language.parser.antlr.SolverLanguageTokenDefInjectingParser | ||
7 | import org.eclipse.viatra.solver.language.serializer.SolverLanguageSyntheticTokenSyntacticSequencer | ||
6 | 8 | ||
7 | /** | 9 | /** |
8 | * Use this class to register components to be used at runtime / without the Equinox extension registry. | 10 | * Use this class to register components to be used at runtime / without the Equinox extension registry. |
9 | */ | 11 | */ |
10 | class SolverLanguageRuntimeModule extends AbstractSolverLanguageRuntimeModule { | 12 | class SolverLanguageRuntimeModule extends AbstractSolverLanguageRuntimeModule { |
13 | |||
14 | override bindIParser() { | ||
15 | SolverLanguageTokenDefInjectingParser | ||
16 | } | ||
17 | |||
18 | |||
19 | override bindISyntacticSequencer() { | ||
20 | SolverLanguageSyntheticTokenSyntacticSequencer | ||
21 | } | ||
22 | |||
11 | } | 23 | } |
diff --git a/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/parser/antlr/SolverLanguageTokenDefInjectingParser.xtend b/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/parser/antlr/SolverLanguageTokenDefInjectingParser.xtend new file mode 100644 index 00000000..87efd2b3 --- /dev/null +++ b/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/parser/antlr/SolverLanguageTokenDefInjectingParser.xtend | |||
@@ -0,0 +1,16 @@ | |||
1 | package org.eclipse.viatra.solver.language.parser.antlr | ||
2 | |||
3 | import com.google.inject.Inject | ||
4 | import org.antlr.runtime.CharStream | ||
5 | import org.eclipse.xtext.parser.antlr.ITokenDefProvider | ||
6 | |||
7 | class SolverLanguageTokenDefInjectingParser extends SolverLanguageParser { | ||
8 | @Inject ITokenDefProvider tokenDefProvider | ||
9 | |||
10 | override protected createLexer(CharStream stream) { | ||
11 | val tokenSource = super.createLexer(stream) as SolverLanguageTokenSource | ||
12 | tokenSource.initializeTokenDefsFrom(tokenDefProvider) | ||
13 | tokenSource | ||
14 | } | ||
15 | |||
16 | } | ||
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 new file mode 100644 index 00000000..4c1dacd5 --- /dev/null +++ b/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/parser/antlr/SolverLanguageTokenSource.java | |||
@@ -0,0 +1,122 @@ | |||
1 | /* | ||
2 | * generated by Xtext 2.21.0 | ||
3 | */ | ||
4 | package org.eclipse.viatra.solver.language.parser.antlr; | ||
5 | |||
6 | import static org.eclipse.viatra.solver.language.parser.antlr.internal.InternalSolverLanguageParser.RULE_FULL_STOP; | ||
7 | import static org.eclipse.viatra.solver.language.parser.antlr.internal.InternalSolverLanguageParser.RULE_ID; | ||
8 | import static org.eclipse.viatra.solver.language.parser.antlr.internal.InternalSolverLanguageParser.RULE_INT; | ||
9 | import static org.eclipse.viatra.solver.language.parser.antlr.internal.InternalSolverLanguageParser.RULE_ML_COMMENT; | ||
10 | import static org.eclipse.viatra.solver.language.parser.antlr.internal.InternalSolverLanguageParser.RULE_QUOTED_ID; | ||
11 | import static org.eclipse.viatra.solver.language.parser.antlr.internal.InternalSolverLanguageParser.RULE_REFLEXIVE_TRANSITIVE_CLOSURE; | ||
12 | import static org.eclipse.viatra.solver.language.parser.antlr.internal.InternalSolverLanguageParser.RULE_SL_COMMENT; | ||
13 | import static org.eclipse.viatra.solver.language.parser.antlr.internal.InternalSolverLanguageParser.RULE_TRANSITIVE_CLOSURE; | ||
14 | import static org.eclipse.viatra.solver.language.parser.antlr.internal.InternalSolverLanguageParser.RULE_WS; | ||
15 | |||
16 | import java.util.Map; | ||
17 | |||
18 | import org.antlr.runtime.Token; | ||
19 | import org.antlr.runtime.TokenSource; | ||
20 | import org.eclipse.xtext.parser.antlr.ITokenDefProvider; | ||
21 | import org.eclipse.xtext.parser.antlr.TokenAcceptor; | ||
22 | |||
23 | public class SolverLanguageTokenSource implements TokenSource { | ||
24 | private TokenSource delegate; | ||
25 | |||
26 | private final TokenAcceptor acceptor = new TokenAcceptor(); | ||
27 | |||
28 | private int lastTokenId = -1; | ||
29 | private int plusTokenId = -1; | ||
30 | private int starTokenId = -1; | ||
31 | private int dotTokenId = -1; | ||
32 | private int openParenTokenId = -1; | ||
33 | |||
34 | public SolverLanguageTokenSource(TokenSource delegate) { | ||
35 | this.delegate = delegate; | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | public String getSourceName() { | ||
40 | return "[SolverLanguageSyntheticTokenTokenSource] " + delegate.getSourceName(); | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public Token nextToken() { | ||
45 | Token token = internalNextToken(); | ||
46 | lastTokenId = token.getType(); | ||
47 | return token; | ||
48 | } | ||
49 | |||
50 | protected Token internalNextToken() { | ||
51 | if (acceptor.hasNext()) { | ||
52 | return acceptor.next(); | ||
53 | } | ||
54 | Token token = delegate.nextToken(); | ||
55 | int type = token.getType(); | ||
56 | if (type == plusTokenId) { | ||
57 | if ((lastTokenId == RULE_ID || lastTokenId == RULE_QUOTED_ID) | ||
58 | && peekUntilVisible().getType() == openParenTokenId) { | ||
59 | token.setType(RULE_TRANSITIVE_CLOSURE); | ||
60 | } | ||
61 | } else if (type == starTokenId) { | ||
62 | if ((lastTokenId == RULE_ID || lastTokenId == RULE_QUOTED_ID) | ||
63 | && peekUntilVisible().getType() == openParenTokenId) { | ||
64 | token.setType(RULE_REFLEXIVE_TRANSITIVE_CLOSURE); | ||
65 | } | ||
66 | } else if (type == dotTokenId) { | ||
67 | if ((lastTokenId != RULE_ID && lastTokenId != RULE_INT) || peekToken().getType() != lastTokenId) { | ||
68 | token.setType(RULE_FULL_STOP); | ||
69 | } | ||
70 | } | ||
71 | return token; | ||
72 | } | ||
73 | |||
74 | protected Token peekUntilVisible() { | ||
75 | Token followingToken = null; | ||
76 | do { | ||
77 | followingToken = peekToken(); | ||
78 | } while (isHidden(followingToken.getType())); | ||
79 | return followingToken; | ||
80 | } | ||
81 | |||
82 | protected Token peekToken() { | ||
83 | Token followingToken = delegate.nextToken(); | ||
84 | acceptor.accept(followingToken); | ||
85 | return followingToken; | ||
86 | } | ||
87 | |||
88 | protected boolean isHidden(int tokenType) { | ||
89 | return tokenType == RULE_WS || tokenType == RULE_SL_COMMENT || tokenType == RULE_ML_COMMENT; | ||
90 | } | ||
91 | |||
92 | public void initializeTokenDefsFrom(ITokenDefProvider provider) { | ||
93 | for (Map.Entry<Integer, String> entry : provider.getTokenDefMap().entrySet()) { | ||
94 | switch (entry.getValue()) { | ||
95 | case "'+'": | ||
96 | plusTokenId = entry.getKey(); | ||
97 | break; | ||
98 | case "'*'": | ||
99 | starTokenId = entry.getKey(); | ||
100 | break; | ||
101 | case "'.'": | ||
102 | dotTokenId = entry.getKey(); | ||
103 | break; | ||
104 | case "'('": | ||
105 | openParenTokenId = entry.getKey(); | ||
106 | break; | ||
107 | } | ||
108 | } | ||
109 | if (plusTokenId == -1) { | ||
110 | throw new IllegalStateException("Token '+' was not found"); | ||
111 | } | ||
112 | if (starTokenId == -1) { | ||
113 | throw new IllegalStateException("Token '*' was not found"); | ||
114 | } | ||
115 | if (dotTokenId == -1) { | ||
116 | throw new IllegalStateException("Token '.' was not found"); | ||
117 | } | ||
118 | if (openParenTokenId == -1) { | ||
119 | throw new IllegalStateException("Token '(' was not found"); | ||
120 | } | ||
121 | } | ||
122 | } | ||
diff --git a/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/serializer/SolverLanguageSyntheticTokenSyntacticSequencer.xtend b/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/serializer/SolverLanguageSyntheticTokenSyntacticSequencer.xtend new file mode 100644 index 00000000..587ea27b --- /dev/null +++ b/Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/serializer/SolverLanguageSyntheticTokenSyntacticSequencer.xtend | |||
@@ -0,0 +1,21 @@ | |||
1 | package org.eclipse.viatra.solver.language.serializer | ||
2 | |||
3 | import org.eclipse.emf.ecore.EObject | ||
4 | import org.eclipse.xtext.RuleCall | ||
5 | import org.eclipse.xtext.nodemodel.INode | ||
6 | |||
7 | class SolverLanguageSyntheticTokenSyntacticSequencer extends SolverLanguageSyntacticSequencer { | ||
8 | |||
9 | override protected getTRANSITIVE_CLOSUREToken(EObject semanticObject, RuleCall ruleCall, INode node) { | ||
10 | "+" | ||
11 | } | ||
12 | |||
13 | override protected getREFLEXIVE_TRANSITIVE_CLOSUREToken(EObject semanticObject, RuleCall ruleCall, INode node) { | ||
14 | "*" | ||
15 | } | ||
16 | |||
17 | override protected getFULL_STOPToken(EObject semanticObject, RuleCall ruleCall, INode node) { | ||
18 | "." | ||
19 | } | ||
20 | |||
21 | } \ No newline at end of file | ||