-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Add action templates functionality #4345
Open
DavidGregory084
wants to merge
9
commits into
antlr:dev
Choose a base branch
from
opencastsoftware:action-templates
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b28a9f4
WIP: Add action templates functionality
DavidGregory084 573908d
Improve variable naming in action templates test suite
DavidGregory084 616d764
More consistent naming in action templates error messages
DavidGregory084 f89c520
Integrate ST error reporting with ANTLR's grammar error reporting
DavidGregory084 8a8a063
Fix incorrect error message in action templates group error listener,…
DavidGregory084 ef95015
Add a test for action templates in a semantic predicate
DavidGregory084 84866cc
Lookup action templates from inputDirectory, grammar file directory a…
DavidGregory084 c7dc4c8
Add documentation for the `actionTemplates` grammar option
DavidGregory084 0ae16da
Fix a bug in passing the path to the StringTemplate group file, add a…
DavidGregory084 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
tool-testsuite/test/org/antlr/v4/test/tool/TestActionTemplates.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package org.antlr.v4.test.tool; | ||
|
||
import org.antlr.v4.test.runtime.RunOptions; | ||
import org.antlr.v4.test.runtime.Stage; | ||
import org.antlr.v4.test.runtime.java.JavaRunner; | ||
import org.antlr.v4.test.runtime.states.ExecutedState; | ||
import org.antlr.v4.test.runtime.states.GeneratedState; | ||
import org.antlr.v4.test.runtime.states.State; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import java.nio.file.Path; | ||
|
||
import static org.antlr.v4.test.runtime.FileUtils.writeFile; | ||
import static org.antlr.v4.test.runtime.RuntimeTestUtils.FileSeparator; | ||
import static org.antlr.v4.test.tool.ToolTestUtils.createOptionsForJavaToolTests; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class TestActionTemplates { | ||
@Test() void testIncorrectActionTemplateGroupExtension(@TempDir Path tempDir) { | ||
String actionTemplates = tempDir + FileSeparator + "Java.st"; | ||
|
||
String grammar = | ||
"lexer grammar L;"+ | ||
"WS : (' '|'\\n') -> skip ;"; | ||
|
||
State state = execLexer(grammar, "34 34", tempDir, actionTemplates); | ||
|
||
assertInstanceOf(GeneratedState.class, state); | ||
GeneratedState generated = (GeneratedState) state; | ||
assertTrue(generated.containsErrors()); | ||
assertEquals( | ||
"State: Generate; \n" + | ||
"error(207): error reading action templates file " + actionTemplates + ": " + | ||
"Group file names must end in .stg: " + actionTemplates + "\n", | ||
generated.getErrorMessage()); | ||
} | ||
|
||
@Test() void testActionTemplateFileMissing(@TempDir Path tempDir) { | ||
String actionTemplates = tempDir + FileSeparator + "Java.stg"; | ||
|
||
String grammar = | ||
"lexer grammar L;"+ | ||
"WS : (' '|'\\n') -> skip ;"; | ||
|
||
State state = execLexer(grammar, "34 34", tempDir, actionTemplates); | ||
|
||
assertInstanceOf(GeneratedState.class, state); | ||
GeneratedState generated = (GeneratedState) state; | ||
assertTrue(generated.containsErrors()); | ||
assertEquals( | ||
"State: Generate; \n" + | ||
"error(206): cannot find action templates file " + actionTemplates + " given for L\n", | ||
generated.getErrorMessage()); | ||
} | ||
|
||
@Test void testActionTemplateLexerAction(@TempDir Path tempDir) { | ||
writeActionTemplatesFile(tempDir, "writeln(s) ::= <<outStream.println(\"<s>\");>>"); | ||
|
||
String actionTemplates = tempDir + FileSeparator + "Java.stg"; | ||
|
||
String grammar = | ||
"lexer grammar L;\n"+ | ||
"I : '0'..'9'+ {<writeln(\"I\")>} ;\n"+ | ||
"WS : (' '|'\\n') -> skip ;"; | ||
|
||
State state = execLexer(grammar, "34 34", tempDir, actionTemplates); | ||
|
||
// Should have identical output to TestLexerActions.testActionExecutedInDFA | ||
String expecting = | ||
"I\n" + | ||
"I\n" + | ||
"[@0,0:1='34',<1>,1:0]\n" + | ||
"[@1,3:4='34',<1>,1:3]\n" + | ||
"[@2,5:4='<EOF>',<-1>,1:5]\n"; | ||
|
||
assertInstanceOf(ExecutedState.class, state); | ||
|
||
assertEquals(expecting, ((ExecutedState) state).output); | ||
} | ||
|
||
@Test void testActionTemplateHeader(@TempDir Path tempDir) { | ||
String actionTemplates = | ||
"normalizerImports() ::= <<\n" + | ||
"import java.text.Normalizer;\n" + | ||
"import java.text.Normalizer.Form;\n" + | ||
">>\n" + | ||
"normalize(s) ::= <<Normalizer.normalize(<s>, Form.NFKC)>>\n" + | ||
"getText() ::= <<getText()>>\n" + | ||
"setText(s) ::= <<setText(<s>);>>"; | ||
|
||
writeActionTemplatesFile(tempDir, actionTemplates); | ||
|
||
String actionTemplatesFile = tempDir + FileSeparator + "Java.stg"; | ||
|
||
String grammar = | ||
"lexer grammar L;\n"+ | ||
"@lexer::header {\n"+ | ||
"<normalizerImports()>\n"+ | ||
"}\n"+ | ||
"ID : (ID_START ID_CONTINUE* | '_' ID_CONTINUE+) { <setText(normalize(getText()))> } ;\n"+ | ||
"ID_START : [\\p{XID_Start}] ;\n"+ | ||
"ID_CONTINUE: [\\p{XID_Continue}] ;\n"+ | ||
"WS : (' '|'\\n') -> skip ;"; | ||
|
||
State state = execLexer(grammar, "This _is \ufb01ne", tempDir, actionTemplatesFile); | ||
|
||
String expecting = | ||
"[@0,0:3='This',<1>,1:0]\n"+ | ||
"[@1,5:7='_is',<1>,1:5]\n"+ | ||
"[@2,9:11='fine',<1>,1:9]\n"+ | ||
"[@3,12:11='<EOF>',<-1>,1:12]\n"; | ||
|
||
assertInstanceOf(ExecutedState.class, state); | ||
|
||
assertEquals(expecting, ((ExecutedState) state).output); | ||
} | ||
|
||
void writeActionTemplatesFile(Path tempDir, String template) { | ||
writeFile(tempDir.toString(), "Java.stg", template); | ||
} | ||
|
||
State execLexer(String grammarStr, String input, Path tempDir, String actionTemplates) { | ||
RunOptions runOptions = createOptionsForJavaToolTests("L.g4", grammarStr, null, "L", | ||
false, true, null, input, | ||
false, false, Stage.Execute, actionTemplates); | ||
try (JavaRunner runner = new JavaRunner(tempDir, false)) { | ||
return runner.run(runOptions); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,6 +80,7 @@ public class Grammar implements AttributeResolver { | |
parserOptions.add("TokenLabelType"); | ||
parserOptions.add("tokenVocab"); | ||
parserOptions.add("language"); | ||
parserOptions.add("actionTemplates"); | ||
parserOptions.add("accessLevel"); | ||
parserOptions.add("exportMacro"); | ||
parserOptions.add(caseInsensitiveOptionName); | ||
|
@@ -1178,6 +1179,10 @@ public String getLanguage() { | |
return getOptionString("language"); | ||
} | ||
|
||
public String getActionTemplates() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
return getOptionString("actionTemplates"); | ||
} | ||
|
||
public String getOptionString(String key) { return ast.getOptionString(key); } | ||
|
||
/** Given ^(TOKEN_REF ^(OPTIONS ^(ELEMENT_OPTIONS (= assoc right)))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure we want to support this as a grammar option, it should be a cmd line option only