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/solver/language/parser/antlr/SolverLanguageTokenSource.java | |
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/solver/language/parser/antlr/SolverLanguageTokenSource.java')
-rw-r--r-- | Application/org.eclipse.viatra.solver.language/src/org/eclipse/viatra/solver/language/parser/antlr/SolverLanguageTokenSource.java | 122 |
1 files changed, 122 insertions, 0 deletions
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 | } | ||