Skip to content

The Unified and Autonomous Development Platform

midwork

robin shine9d20c9742291
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Altenative.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Alternative.java
similarity index 74%
rename from commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Altenative.java
rename to commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Alternative.java
index 42ada95b5c..9faa5fbf9a 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Altenative.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Alternative.java
@@ -2,7 +2,7 @@ package com.pmease.commons.antlr.grammar;
import java.util.List;
-public class Altenative extends GrammarMember {
+public class Alternative extends GrammarMember {
private static final long serialVersionUID = 1L;
@@ -10,7 +10,7 @@ public class Altenative extends GrammarMember {
private final List<Element> elements;
- public Altenative(Grammar grammar, String label, List<Element> elements) {
+ public Alternative(Grammar grammar, String label, List<Element> elements) {
super(grammar);
this.label = label;
this.elements = elements;
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/BlockElement.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/BlockElement.java
index 413c0135ba..ddf9541b87 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/BlockElement.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/BlockElement.java
@@ -6,15 +6,15 @@ public class BlockElement extends Element {
private static final long serialVersionUID = 1L;
- private final List<Altenative> altenatives;
+ private final List<Alternative> altenatives;
- public BlockElement(Grammar grammar, String label, Multiplicity multiplicity, List<Altenative> altenatives) {
+ public BlockElement(Grammar grammar, String label, Multiplicity multiplicity, List<Alternative> altenatives) {
super(grammar, label, multiplicity);
this.altenatives = altenatives;
}
- public List<Altenative> getAltenatives() {
+ public List<Alternative> getAltenatives() {
return altenatives;
}
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Element.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Element.java
index 035f0fe215..f7adbeb4f0 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Element.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Element.java
@@ -4,7 +4,7 @@ public class Element extends GrammarMember {
private static final long serialVersionUID = 1L;
- public enum Multiplicity{ONE, ONE_OR_ZERO, ZERO_OR_MORE, ONE_OR_MORE};
+ public enum Multiplicity{ONE, ZERO_OR_ONE, ZERO_OR_MORE, ONE_OR_MORE};
private final String label;
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Grammar.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Grammar.java
index 78c866a4c7..9282f425d3 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Grammar.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Grammar.java
@@ -16,9 +16,8 @@ import javax.annotation.Nullable;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.CommonTokenStream;
-import org.antlr.v4.runtime.Parser;
+import org.antlr.v4.runtime.tree.TerminalNode;
-import com.google.common.base.Preconditions;
import com.pmease.commons.antlr.ANTLRv4Lexer;
import com.pmease.commons.antlr.ANTLRv4Parser;
import com.pmease.commons.antlr.ANTLRv4Parser.AlternativeContext;
@@ -47,19 +46,24 @@ public class Grammar implements Serializable {
private static final long serialVersionUID = 1L;
- private static final String PARSER_SUFFIX = "Parser";
-
private final Map<String, Rule> rules = new HashMap<>();
private final Map<String, Integer> tokenTypesByLiteral = new HashMap<>();
private final Map<String, Integer> tokenTypesByRule = new HashMap<>();
- public Grammar(Class<? extends Parser> parserClass) {
- String parserName = parserClass.getSimpleName();
- Preconditions.checkArgument(parserName.endsWith(PARSER_SUFFIX));
- String grammarName = parserName.substring(0, parserName.length() - PARSER_SUFFIX.length());
- try (InputStream is = getClass().getClassLoader().getResourceAsStream(grammarName + ".tokens")) {
+ /**
+ * Construct object representation of ANTLR grammar file.
+ *
+ * @param grammarFiles
+ * grammar files in class path, relative to class path root
+ * @param tokenFile
+ * generated tokens file in class path, relative to class path root
+ */
+ public Grammar(String grammarFiles[], String tokenFile) {
+ tokenTypesByRule.put("EOF", -1);
+
+ try (InputStream is = getClass().getClassLoader().getResourceAsStream(tokenFile)) {
Properties props = new Properties();
props.load(is);
for (Map.Entry<Object, Object> entry: props.entrySet()) {
@@ -73,42 +77,44 @@ public class Grammar implements Serializable {
} catch (IOException e) {
throw new RuntimeException(e);
}
-
- try (InputStream is = parserClass.getResourceAsStream(grammarName + ".g4")) {
- ANTLRv4Lexer lexer = new ANTLRv4Lexer(new ANTLRInputStream(is));
- CommonTokenStream tokens = new CommonTokenStream(lexer);
- ANTLRv4Parser parser = new ANTLRv4Parser(tokens);
- parser.removeErrorListeners();
- parser.setErrorHandler(new BailErrorStrategy());
- for (RuleSpecContext ruleSpecContext: parser.rules().ruleSpec()) {
- Rule rule = newRule(ruleSpecContext);
- rules.put(rule.getName(), rule);
+
+ for (String grammarFile: grammarFiles) {
+ try (InputStream is = getClass().getClassLoader().getResourceAsStream(grammarFile)) {
+ ANTLRv4Lexer lexer = new ANTLRv4Lexer(new ANTLRInputStream(is));
+ CommonTokenStream tokens = new CommonTokenStream(lexer);
+ ANTLRv4Parser parser = new ANTLRv4Parser(tokens);
+ parser.removeErrorListeners();
+ parser.setErrorHandler(new BailErrorStrategy());
+ for (RuleSpecContext ruleSpecContext: parser.grammarSpec().rules().ruleSpec()) {
+ Rule rule = newRule(ruleSpecContext);
+ rules.put(rule.getName(), rule);
+ }
+ } catch (IOException e) {
+ throw new RuntimeException(e);
}
- } catch (IOException e) {
- throw new RuntimeException(e);
}
}
private Rule newRule(RuleSpecContext ruleSpecContext) {
String name;
- List<Altenative> altenatives = new ArrayList<>();
+ List<Alternative> alternatives = new ArrayList<>();
ParserRuleSpecContext parserRuleSpecContext = ruleSpecContext.parserRuleSpec();
if (parserRuleSpecContext != null) {
name = parserRuleSpecContext.RULE_REF().getText();
RuleBlockContext ruleBlockContext = parserRuleSpecContext.ruleBlock();
for (LabeledAltContext labeledAltContext: ruleBlockContext.ruleAltList().labeledAlt())
- altenatives.add(newAltenative(labeledAltContext));
+ alternatives.add(newAltenative(labeledAltContext));
} else {
LexerRuleSpecContext lexerRuleSpecContext = ruleSpecContext.lexerRuleSpec();
name = lexerRuleSpecContext.TOKEN_REF().getText();
LexerRuleBlockContext lexerRuleBlockContext = lexerRuleSpecContext.lexerRuleBlock();
for (LexerAltContext lexerAltContext: lexerRuleBlockContext.lexerAltList().lexerAlt())
- altenatives.add(newAltenative(lexerAltContext));
+ alternatives.add(newAltenative(lexerAltContext));
}
- return new Rule(this, name, altenatives);
+ return new Rule(this, name, alternatives);
}
- private Altenative newAltenative(LexerAltContext lexerAltContext) {
+ private Alternative newAltenative(LexerAltContext lexerAltContext) {
List<Element> elements = new ArrayList<>();
if (lexerAltContext.lexerElements() != null) {
for (LexerElementContext lexerElementContext: lexerAltContext.lexerElements().lexerElement()) {
@@ -118,10 +124,10 @@ public class Grammar implements Serializable {
}
}
- return new Altenative(this, null, elements);
+ return new Alternative(this, null, elements);
}
- private Altenative newAltenative(LabeledAltContext labeledAltContext) {
+ private Alternative newAltenative(LabeledAltContext labeledAltContext) {
String label;
if (labeledAltContext.id() != null)
label = labeledAltContext.id().getText();
@@ -149,7 +155,7 @@ public class Grammar implements Serializable {
}
}
- private Altenative newAltenative(@Nullable String label, AlternativeContext alternativeContext) {
+ private Alternative newAltenative(@Nullable String label, AlternativeContext alternativeContext) {
List<Element> elements = new ArrayList<>();
for (ElementContext elementContext: alternativeContext.element()) {
Element element = newElement(elementContext);
@@ -157,7 +163,7 @@ public class Grammar implements Serializable {
elements.add(element);
}
- return new Altenative(this, label, elements);
+ return new Alternative(this, label, elements);
}
@Nullable
@@ -187,7 +193,7 @@ public class Grammar implements Serializable {
int tokenType = tokenTypesByRule.get(ruleName);
return new LexerRuleElement(this, label, multiplicity, tokenType, ruleName);
} else {
- String literal = atomContext.terminal().STRING_LITERAL().getText();
+ String literal = getLiteral(atomContext.terminal().STRING_LITERAL());
int tokenType = tokenTypesByLiteral.get(literal);
return new LiteralElement(this, label, multiplicity, tokenType, literal);
}
@@ -201,6 +207,11 @@ public class Grammar implements Serializable {
throw new IllegalStateException();
}
}
+
+ private String getLiteral(TerminalNode terminal) {
+ String literal = terminal.getText();
+ return literal.substring(1, literal.length()-1);
+ }
private Element newElement(String label, LexerAtomContext lexerAtomContext, EbnfSuffixContext ebnfSuffixContext) {
Multiplicity multiplicity = newMultiplicity(ebnfSuffixContext);
@@ -212,7 +223,7 @@ public class Grammar implements Serializable {
tokenType = 0;
return new LexerRuleElement(this, label, multiplicity, tokenType, ruleName);
} else {
- String literal = lexerAtomContext.terminal().STRING_LITERAL().getText();
+ String literal = getLiteral(lexerAtomContext.terminal().STRING_LITERAL());
Integer tokenType = tokenTypesByLiteral.get(literal);
if (tokenType == null)
tokenType = 0;
@@ -244,7 +255,7 @@ public class Grammar implements Serializable {
private int getTokenType(SetElementContext setElementContext) {
Integer tokenType;
if (setElementContext.STRING_LITERAL() != null)
- tokenType = tokenTypesByLiteral.get(setElementContext.STRING_LITERAL().getText());
+ tokenType = tokenTypesByLiteral.get(getLiteral(setElementContext.STRING_LITERAL()));
else if (setElementContext.TOKEN_REF() != null)
tokenType = tokenTypesByRule.get(setElementContext.TOKEN_REF().getText());
else
@@ -262,24 +273,24 @@ public class Grammar implements Serializable {
else if (ebnfSuffixContext.PLUS() != null)
return Multiplicity.ONE_OR_MORE;
else
- return Multiplicity.ONE_OR_ZERO;
+ return Multiplicity.ZERO_OR_ONE;
} else {
return Multiplicity.ONE;
}
}
private Element newElement(@Nullable String label, BlockContext blockContext, @Nullable EbnfSuffixContext ebnfSuffixContext) {
- List<Altenative> altenatives = new ArrayList<>();
+ List<Alternative> alternatives = new ArrayList<>();
for (AlternativeContext alternativeContext: blockContext.altList().alternative())
- altenatives.add(newAltenative(null, alternativeContext));
- return new BlockElement(this, label, newMultiplicity(ebnfSuffixContext), altenatives);
+ alternatives.add(newAltenative(null, alternativeContext));
+ return new BlockElement(this, label, newMultiplicity(ebnfSuffixContext), alternatives);
}
private Element newElement(@Nullable String label, LexerBlockContext lexerBlockContext, @Nullable EbnfSuffixContext ebnfSuffixContext) {
- List<Altenative> altenatives = new ArrayList<>();
+ List<Alternative> alternatives = new ArrayList<>();
for (LexerAltContext lexerAltContext: lexerBlockContext.lexerAltList().lexerAlt())
- altenatives.add(newAltenative(lexerAltContext));
- return new BlockElement(this, label, newMultiplicity(ebnfSuffixContext), altenatives);
+ alternatives.add(newAltenative(lexerAltContext));
+ return new BlockElement(this, label, newMultiplicity(ebnfSuffixContext), alternatives);
}
private Element newElement(EbnfContext ebnfContext) {
diff --git a/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Rule.java b/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Rule.java
index 956be86471..cedebeea63 100644
--- a/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Rule.java
+++ b/commons.antlr/src/main/java/com/pmease/commons/antlr/grammar/Rule.java
@@ -8,9 +8,9 @@ public class Rule extends GrammarMember {
private final String name;
- private final List<Altenative> altenatives;
+ private final List<Alternative> altenatives;
- public Rule(Grammar grammar, String name, List<Altenative> altenatives) {
+ public Rule(Grammar grammar, String name, List<Alternative> altenatives) {
super(grammar);
this.name = name;
@@ -21,7 +21,7 @@ public class Rule extends GrammarMember {
return name;
}
- public List<Altenative> getAltenatives() {
+ public List<Alternative> getAlternatives() {
return altenatives;
}
diff --git a/commons.antlr/src/test/java/com/pmease/commons/antlr/grammar/GrammarTest.java b/commons.antlr/src/test/java/com/pmease/commons/antlr/grammar/GrammarTest.java
new file mode 100644
index 0000000000..bab06513b4
--- /dev/null
+++ b/commons.antlr/src/test/java/com/pmease/commons/antlr/grammar/GrammarTest.java
@@ -0,0 +1,64 @@
+package com.pmease.commons.antlr.grammar;
+
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+import com.pmease.commons.antlr.grammar.Element.Multiplicity;
+
+public class GrammarTest {
+
+ private Grammar grammar = new Grammar(
+ new String[]{
+ "com/pmease/commons/antlr/ANTLRv4Parser.g4",
+ "com/pmease/commons/antlr/ANTLRv4Lexer.g4",
+ "com/pmease/commons/antlr/LexBasic.g4",
+ "com/pmease/commons/antlr/LexUnicode.g4"
+ }, "ANTLRv4Parser.tokens");
+
+ @Test
+ public void testGrammarSpec() {
+ Rule rule = grammar.getRules().get("grammarSpec");
+ assertEquals(1, rule.getAlternatives().size());
+ Alternative alternative = rule.getAlternatives().get(0);
+ assertEquals(8, alternative.getElements().size());
+ LexerRuleElement lexerRuleElement = (LexerRuleElement) alternative.getElements().get(0);
+ assertEquals("DOC_COMMENT", lexerRuleElement.getRuleName());
+ assertEquals(4, lexerRuleElement.getTokenType());
+ assertEquals(Multiplicity.ZERO_OR_ONE, lexerRuleElement.getMultiplicity());
+
+ RuleElement ruleElement = (RuleElement) alternative.getElements().get(1);
+ assertEquals("grammarType", ruleElement.getRuleName());
+ assertEquals(Multiplicity.ONE, ruleElement.getMultiplicity());
+
+ ruleElement = (RuleElement) alternative.getElements().get(6);
+ assertEquals("modeSpec", ruleElement.getRuleName());
+ assertEquals(Multiplicity.ZERO_OR_MORE, ruleElement.getMultiplicity());
+ }
+
+ @Test
+ public void testDocComment() {
+ Rule rule = grammar.getRules().get("DocComment");
+ assertEquals(1, rule.getAlternatives().size());
+ Alternative alternative = rule.getAlternatives().get(0);
+ assertEquals(3, alternative.getElements().size());
+ LiteralElement literalElement = (LiteralElement) alternative.getElements().get(0);
+ assertEquals(0, literalElement.getTokenType());
+ assertEquals("/**", literalElement.getLiteral());
+ assertEquals(Multiplicity.ONE, literalElement.getMultiplicity());
+
+ AnyTokenElement anyTokenElement = (AnyTokenElement) alternative.getElements().get(1);
+ assertEquals(Multiplicity.ZERO_OR_MORE, anyTokenElement.getMultiplicity());
+
+ BlockElement blockElement = (BlockElement) alternative.getElements().get(2);
+ assertEquals(2, blockElement.getAltenatives().size());
+ assertEquals(1, blockElement.getAltenatives().get(0).getElements().size());
+ literalElement = (LiteralElement) blockElement.getAltenatives().get(0).getElements().get(0);
+ assertEquals(0, literalElement.getTokenType());
+ assertEquals("*/", literalElement.getLiteral());
+ assertEquals(1, blockElement.getAltenatives().get(1).getElements().size());
+ LexerRuleElement lexerRuleElement = (LexerRuleElement) blockElement.getAltenatives().get(1).getElements().get(0);
+ assertEquals(-1, lexerRuleElement.getTokenType());
+ assertEquals("EOF", lexerRuleElement.getRuleName());
+ }
+
+}
diff --git a/gitplex.product/src/test/java/gitplex/product/Test.java b/gitplex.product/src/test/java/gitplex/product/Test.java
index d96a7c6a8c..fed65dfeda 100644
--- a/gitplex.product/src/test/java/gitplex/product/Test.java
+++ b/gitplex.product/src/test/java/gitplex/product/Test.java
@@ -8,17 +8,16 @@ import org.antlr.v4.runtime.CommonTokenStream;
import com.pmease.commons.antlr.ANTLRv4Lexer;
import com.pmease.commons.antlr.ANTLRv4Parser;
-import com.pmease.gitplex.web.page.repository.commit.CommitQueryParser;
public class Test {
@org.junit.Test
public void test() throws IOException {
- InputStream is = CommitQueryParser.class.getResourceAsStream("CommitQuery.g4");
+ InputStream is = ANTLRv4Parser.class.getResourceAsStream("ANTLRv4Lexer.g4");
ANTLRv4Lexer lexer = new ANTLRv4Lexer(new ANTLRInputStream(is));
CommonTokenStream tokens = new CommonTokenStream(lexer);
ANTLRv4Parser parser = new ANTLRv4Parser(tokens);
- System.out.println(parser.rules().ruleSpec().size());
+ System.out.println(parser.grammarSpec().rules().ruleSpec().size());
}
}
\ No newline at end of file