Search in sources :

Example 26 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class TestIntervalSet method testRangeAndIsolatedElement.

@Test
public void testRangeAndIsolatedElement() throws Exception {
    IntervalSet s = IntervalSet.of('a', 'z');
    IntervalSet s2 = IntervalSet.of('d');
    String expecting = "100";
    String result = (s.and(s2)).toString();
    assertEquals(expecting, result);
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Test(org.junit.Test)

Example 27 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class Antlr4Mojo method processGrammarFiles.

/**
     *
     * @param sourceDirectory
     * @exception InclusionScanException
     */
private List<List<String>> processGrammarFiles(List<String> args, Set<File> grammarFiles, GrammarDependencies dependencies, File sourceDirectory) throws InclusionScanException, IOException {
    // We don't want the plugin to run for every grammar, regardless of whether
    // it's changed since the last compilation. Check the mtime of the tokens vs
    // the grammar file mtime to determine whether we even need to execute.
    Set<File> grammarFilesToProcess = new HashSet<File>();
    for (File grammarFile : grammarFiles) {
        String tokensFileName = grammarFile.getName().split("\\.")[0] + ".tokens";
        File outputFile = new File(outputDirectory, tokensFileName);
        if ((!outputFile.exists()) || outputFile.lastModified() < grammarFile.lastModified() || dependencies.isDependencyChanged(grammarFile)) {
            grammarFilesToProcess.add(grammarFile);
        }
    }
    grammarFiles = grammarFilesToProcess;
    if (grammarFiles.isEmpty()) {
        getLog().info("No grammars to process");
        return Collections.emptyList();
    }
    MultiMap<String, File> grammarFileByFolder = new MultiMap<String, File>();
    // grammars to process.
    for (File grammarFile : grammarFiles) {
        if (!buildContext.hasDelta(grammarFile)) {
            continue;
        }
        buildContext.removeMessages(grammarFile);
        getLog().debug("Grammar file '" + grammarFile.getPath() + "' detected.");
        String relPathBase = MojoUtils.findSourceSubdir(sourceDirectory, grammarFile);
        String relPath = relPathBase + grammarFile.getName();
        getLog().debug("  ... relative path is: " + relPath);
        grammarFileByFolder.map(relPathBase, grammarFile);
    }
    List<List<String>> result = new ArrayList<List<String>>();
    for (Map.Entry<String, List<File>> entry : grammarFileByFolder.entrySet()) {
        List<String> folderArgs = new ArrayList<String>(args);
        if (!folderArgs.contains("-package") && !entry.getKey().isEmpty()) {
            folderArgs.add("-package");
            folderArgs.add(getPackageName(entry.getKey()));
        }
        for (File file : entry.getValue()) {
            folderArgs.add(entry.getKey() + file.getName());
        }
        result.add(folderArgs);
    }
    return result;
}
Also used : MultiMap(org.antlr.v4.runtime.misc.MultiMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) Map(java.util.Map) MultiMap(org.antlr.v4.runtime.misc.MultiMap) HashSet(java.util.HashSet)

Example 28 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class TestATNInterpreter method checkMatchedAlt.

public void checkMatchedAlt(LexerGrammar lg, final Grammar g, String inputString, int expected) {
    ATN lexatn = createATN(lg, true);
    LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn, new DFA[] { new DFA(lexatn.modeToStartState.get(Lexer.DEFAULT_MODE)) }, null);
    IntegerList types = getTokenTypesViaATN(inputString, lexInterp);
    //		System.out.println(types);
    g.importVocab(lg);
    ParserATNFactory f = new ParserATNFactory(g);
    ATN atn = f.createATN();
    IntTokenStream input = new IntTokenStream(types);
    //		System.out.println("input="+input.types);
    ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, input);
    ATNState startState = atn.ruleToStartState[g.getRule("a").index];
    if (startState.transition(0).target instanceof BlockStartState) {
        startState = startState.transition(0).target;
    }
    DOTGenerator dot = new DOTGenerator(g);
    //		System.out.println(dot.getDOT(atn.ruleToStartState[g.getRule("a").index]));
    Rule r = g.getRule("e");
    //		if ( r!=null ) System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
    int result = interp.matchATN(input, startState);
    assertEquals(expected, result);
}
Also used : ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) DOTGenerator(org.antlr.v4.tool.DOTGenerator) LexerATNSimulator(org.antlr.v4.runtime.atn.LexerATNSimulator) IntegerList(org.antlr.v4.runtime.misc.IntegerList) ATN(org.antlr.v4.runtime.atn.ATN) Rule(org.antlr.v4.tool.Rule) BlockStartState(org.antlr.v4.runtime.atn.BlockStartState) DFA(org.antlr.v4.runtime.dfa.DFA) ATNState(org.antlr.v4.runtime.atn.ATNState)

Example 29 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class TestATNSerialization method testLexerAction.

@Test
public void testLexerAction() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' {a} ;\n" + "B : 'b' ;\n" + "C : 'c' {c} ;\n");
    String expecting = "max type 3\n" + "0:TOKEN_START -1\n" + "1:RULE_START 0\n" + "2:RULE_STOP 0\n" + "3:RULE_START 1\n" + "4:RULE_STOP 1\n" + "5:RULE_START 2\n" + "6:RULE_STOP 2\n" + "7:BASIC 0\n" + "8:BASIC 0\n" + "9:BASIC 0\n" + "10:BASIC 1\n" + "11:BASIC 1\n" + "12:BASIC 2\n" + "13:BASIC 2\n" + "14:BASIC 2\n" + "rule 0:1 1\n" + "rule 1:3 2\n" + "rule 2:5 3\n" + "mode 0:0\n" + "0->1 EPSILON 0,0,0\n" + "0->3 EPSILON 0,0,0\n" + "0->5 EPSILON 0,0,0\n" + "1->7 EPSILON 0,0,0\n" + "3->10 EPSILON 0,0,0\n" + "5->12 EPSILON 0,0,0\n" + "7->8 ATOM 97,0,0\n" + "8->9 ACTION 0,0,0\n" + "9->2 EPSILON 0,0,0\n" + "10->11 ATOM 98,0,0\n" + "11->4 EPSILON 0,0,0\n" + "12->13 ATOM 99,0,0\n" + "13->14 ACTION 2,1,0\n" + "14->6 EPSILON 0,0,0\n" + "0:0\n";
    ATN atn = createATN(lg, true);
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(lg.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Example 30 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project antlr4 by antlr.

the class TestATNSerialization method testLexerNotSetWithRange2.

@Test
public void testLexerNotSetWithRange2() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "ID : ~('a'|'b') ~('e'|'p'..'t')\n ;");
    String expecting = "max type 1\n" + "0:TOKEN_START -1\n" + "1:RULE_START 0\n" + "2:RULE_STOP 0\n" + "3:BASIC 0\n" + "4:BASIC 0\n" + "5:BASIC 0\n" + "rule 0:1 1\n" + "mode 0:0\n" + "0:'a'..'b'\n" + "1:'e'..'e', 'p'..'t'\n" + "0->1 EPSILON 0,0,0\n" + "1->3 EPSILON 0,0,0\n" + "3->4 NOT_SET 0,0,0\n" + "4->5 NOT_SET 1,0,0\n" + "5->2 EPSILON 0,0,0\n" + "0:0\n";
    ATN atn = createATN(lg, true);
    String result = ATNSerializer.getDecoded(atn, Arrays.asList(lg.getTokenNames()));
    assertEquals(expecting, result);
}
Also used : ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)131 LexerGrammar (org.antlr.v4.tool.LexerGrammar)78 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)52 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)49 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)44 ATN (org.antlr.v4.runtime.atn.ATN)44 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)38 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)38 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)38 ParseTree (org.antlr.v4.runtime.tree.ParseTree)29 ATNState (org.antlr.v4.runtime.atn.ATNState)11 Grammar (org.antlr.v4.tool.Grammar)10 STGroupString (org.stringtemplate.v4.STGroupString)10 ByteBuffer (java.nio.ByteBuffer)8 IntBuffer (java.nio.IntBuffer)8 UTF8CodePointDecoder (org.antlr.v4.runtime.UTF8CodePointDecoder)8 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)8 DOTGenerator (org.antlr.v4.tool.DOTGenerator)8 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)7 Rule (org.antlr.v4.tool.Rule)7