all rewrites working minus error checking ones
[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 8851]
This commit is contained in:
parent
1dc598c26e
commit
dbceac768d
|
@ -1,9 +1,7 @@
|
|||
grammar T;
|
||||
options {output=AST;}
|
||||
|
||||
a : 'var' (ID ':' type ';')+ -> ^('var' ^(':' ID type)*) ;
|
||||
|
||||
type : ID ;
|
||||
a: A b=B B b=B c+=C C c+=C D {String s=$D.text;} -> A* B* C* D ;
|
||||
ID : 'a'..'z'+ ;
|
||||
INT : '0'..'9'+;
|
||||
WS : (' '|'\n') {$channel=HIDDEN;} ;
|
||||
|
|
|
@ -147,7 +147,6 @@ public class ParserASTExtension extends CodeGeneratorExtension {
|
|||
// add code to track rule results in _track_r
|
||||
String trackName = factory.getGenerator().target.getElementListName(invokeOp.ast.getText());
|
||||
TrackRuleElement t = new TrackRuleElement(factory, invokeOp.ast, trackName, label);
|
||||
clearTrackingIfSingularLabel(ops, invokeOp, trackName);
|
||||
ops.add(t);
|
||||
|
||||
// track any explicit label like _track_label but not implicit label
|
||||
|
@ -168,7 +167,6 @@ public class ParserASTExtension extends CodeGeneratorExtension {
|
|||
String trackName = factory.getGenerator().target.getElementListName(matchOp.ast.getText());
|
||||
TrackTokenElement t = new TrackTokenElement(factory, matchOp.ast, trackName,
|
||||
label);
|
||||
clearTrackingIfSingularLabel(ops, matchOp, trackName);
|
||||
ops.add(t);
|
||||
if ( !label.isImplicit ) trackExplicitLabel(ops, label, matchOp);
|
||||
return ops;
|
||||
|
|
|
@ -55,7 +55,7 @@ public class InvokeRule extends RuleElement implements LabeledOp {
|
|||
this.name = ast.getText();
|
||||
CodeGenerator gen = factory.getGenerator();
|
||||
Rule r = factory.getGrammar().getRule(name);
|
||||
ctxName = gen.target.getRuleFunctionContextStructName(factory.getCurrentRuleFunction());
|
||||
ctxName = gen.target.getRuleFunctionContextStructName(r);
|
||||
|
||||
// TODO: move to factory
|
||||
if ( labelAST!=null ) {
|
||||
|
|
|
@ -39,6 +39,7 @@ import org.antlr.v4.tool.*;
|
|||
import org.junit.*;
|
||||
import org.stringtemplate.v4.ST;
|
||||
|
||||
import javax.tools.*;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
|
@ -233,17 +234,56 @@ public abstract class BaseTest {
|
|||
return null;
|
||||
}
|
||||
|
||||
/** Wow! much faster than compiling outside of VM. Finicky though.
|
||||
* Had rules called r and modulo. Wouldn't compile til I changed to 'a'.
|
||||
*/
|
||||
protected boolean compile(String fileName) {
|
||||
String compiler = "javac";
|
||||
String classpathOption = "-classpath";
|
||||
|
||||
String[] args = new String[] {
|
||||
compiler, "-d", tmpdir,
|
||||
"javac", "-d", tmpdir,
|
||||
classpathOption, tmpdir+pathSep+CLASSPATH,
|
||||
tmpdir+"/"+fileName
|
||||
};
|
||||
String cmdLine = compiler+" -d "+tmpdir+" "+classpathOption+" "+tmpdir+pathSep+CLASSPATH+" "+fileName;
|
||||
String cmdLine = "javac" +" -d "+tmpdir+" "+classpathOption+" "+tmpdir+pathSep+CLASSPATH+" "+fileName;
|
||||
//System.out.println("compile: "+cmdLine);
|
||||
|
||||
|
||||
File f = new File(tmpdir, fileName);
|
||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
StandardJavaFileManager fileManager =
|
||||
compiler.getStandardFileManager(null, null, null);
|
||||
|
||||
Iterable<? extends JavaFileObject> compilationUnits =
|
||||
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(f));
|
||||
DiagnosticCollector<JavaFileObject> diagnostics =
|
||||
new DiagnosticCollector<JavaFileObject>();
|
||||
JavaCompiler.CompilationTask task =
|
||||
compiler.getTask(null, fileManager, diagnostics, null, null,
|
||||
compilationUnits);
|
||||
task.call();
|
||||
|
||||
try {
|
||||
fileManager.close();
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
ioe.printStackTrace(System.err);
|
||||
}
|
||||
|
||||
List<String> errors = new ArrayList<String>();
|
||||
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
|
||||
errors.add(
|
||||
String.valueOf(diagnostic.getLineNumber())+
|
||||
": " + diagnostic.getMessage(null));
|
||||
}
|
||||
if ( errors.size()>0 ) {
|
||||
System.err.println("compile stderr from: "+cmdLine);
|
||||
System.err.println(errors);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
/*
|
||||
File outputDir = new File(tmpdir);
|
||||
try {
|
||||
Process process =
|
||||
|
@ -271,8 +311,11 @@ public abstract class BaseTest {
|
|||
e.printStackTrace(System.err);
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Return true if all is ok, no errors */
|
||||
protected boolean antlr(String fileName, String grammarFileName, String grammarStr, boolean debug) {
|
||||
boolean allIsWell = true;
|
||||
|
@ -548,6 +591,17 @@ public abstract class BaseTest {
|
|||
}
|
||||
|
||||
public String execClass(String className) {
|
||||
/* HOW TO GET STDOUT?
|
||||
try {
|
||||
ClassLoader cl_new = new DirectoryLoader(new File(tmpdir));
|
||||
Class compiledClass = cl_new.loadClass(className);
|
||||
Method m = compiledClass.getMethod("main");
|
||||
m.invoke(null);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace(System.err);
|
||||
}
|
||||
*/
|
||||
|
||||
try {
|
||||
String[] args = new String[] {
|
||||
"java", "-classpath", tmpdir+pathSep+CLASSPATH,
|
||||
|
|
|
@ -1005,13 +1005,13 @@ public class TestRewriteAST extends BaseTest {
|
|||
"grammar T; \n" +
|
||||
"options { output = AST; }\n" +
|
||||
"tokens { FLOAT; }\n" +
|
||||
"r\n" +
|
||||
"a\n" +
|
||||
" : INT -> {new CommonTree(new CommonToken(FLOAT,$INT.text+\".0\"))} \n" +
|
||||
" ; \n" +
|
||||
"INT : '0'..'9'+; \n" +
|
||||
"WS: (' ' | '\\n' | '\\t')+ {$channel = HIDDEN;}; \n";
|
||||
String found = execParser("T.g", grammar, "TParser", "TLexer",
|
||||
"r", "25", debug);
|
||||
"a", "25", debug);
|
||||
assertEquals("25.0\n", found);
|
||||
}
|
||||
|
||||
|
@ -1021,14 +1021,14 @@ public class TestRewriteAST extends BaseTest {
|
|||
"options {output=AST;} \n" +
|
||||
"tokens {PARMS;} \n" +
|
||||
"\n" +
|
||||
"modulo \n" +
|
||||
"a \n" +
|
||||
" : 'modulo' ID ('(' parms+ ')')? -> ^('modulo' ID ^(PARMS parms*)?) \n" +
|
||||
" ; \n" +
|
||||
"parms : '#'|ID; \n" +
|
||||
"ID : ('a'..'z' | 'A'..'Z')+;\n" +
|
||||
"WS : (' '|'\\n') {$channel=HIDDEN;} ;\n";
|
||||
String found = execParser("T.g", grammar, "TParser", "TLexer",
|
||||
"modulo", "modulo abc (x y #)", debug);
|
||||
"a", "modulo abc (x y #)", debug);
|
||||
assertEquals("(modulo abc (PARMS x y #))\n", found);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue