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