Created
April 21, 2013 12:26
-
-
Save mattmcd/5429433 to your computer and use it in GitHub Desktop.
ANTLR4 version of Wrapper Generator
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
grammar CFunction; | |
function : retType name args ; | |
args : '(' arg (',' arg)* ')' ; | |
arg | |
: 'double' name # SCALAR_ARG | |
| 'double' '*' name # ARRAY_ARG | |
| 'int' name # LENGTH_ARG | |
; | |
retType | |
: 'double' # SCALAR_RET | |
| 'double' '*' # ARRAY_RET | |
; | |
name : ID; | |
ID : LETTER WORD*; | |
fragment | |
LETTER : 'a'..'z'|'A'..'Z'; | |
fragment | |
DIGIT : '0'..'9'; | |
fragment | |
WORD : LETTER | DIGIT | '_'; | |
WS : (' '|'\r'|'\t'|'\n')+ -> skip; |
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
// Generated from CFunction.g4 by ANTLR 4.0 | |
import org.antlr.v4.runtime.ParserRuleContext; | |
import org.antlr.v4.runtime.Token; | |
import org.antlr.v4.runtime.tree.TerminalNode; | |
import org.antlr.v4.runtime.tree.ErrorNode; | |
import org.antlr.stringtemplate.*; | |
import java.util.List; | |
import java.util.LinkedList; | |
public class CFunctionWalker extends CFunctionBaseListener { | |
List<String> scalarArgs = new LinkedList<String>(); | |
List<String> arrayArgs = new LinkedList<String>(); | |
String functionName; | |
List<String> lengthArgs = new LinkedList<String>(); | |
StringTemplate t; | |
public String out; | |
public CFunctionWalker( StringTemplate t_ ) { | |
t = t_; | |
} | |
public void enterSCALAR_ARG(CFunctionParser.SCALAR_ARGContext ctx) { | |
scalarArgs.add( ctx.name().getText()); | |
} | |
public void enterARRAY_ARG(CFunctionParser.ARRAY_ARGContext ctx) { | |
arrayArgs.add( ctx.name().getText()); | |
} | |
@Override public void enterSCALAR_RET(CFunctionParser.SCALAR_RETContext ctx) { } | |
@Override public void exitSCALAR_RET(CFunctionParser.SCALAR_RETContext ctx) { } | |
@Override public void enterLENGTH_ARG(CFunctionParser.LENGTH_ARGContext ctx) { | |
lengthArgs.add( ctx.name().getText()); | |
} | |
@Override public void enterARRAY_RET(CFunctionParser.ARRAY_RETContext ctx) { } | |
@Override public void exitARRAY_RET(CFunctionParser.ARRAY_RETContext ctx) { } | |
public void enterFunction(CFunctionParser.FunctionContext ctx) { | |
functionName = ctx.name().getText(); | |
} | |
public void exitFunction(CFunctionParser.FunctionContext ctx) { | |
/* | |
System.out.println( "Array arguments : " ); | |
for ( String array : arrayArgs ) System.out.println( array ); | |
System.out.println( "Scalar arguments : " ); | |
for ( String scalar : scalarArgs ) System.out.println( scalar ); | |
*/ | |
t.setAttribute("name", functionName ); | |
t.setAttribute("len", lengthArgs ); | |
t.setAttribute("scalars", scalarArgs ); | |
t.setAttribute("arrays", arrayArgs ); | |
out = t.toString(); | |
} | |
} |
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
import org.antlr.v4.runtime.*; | |
import java.io.FileReader; | |
public class MainV4 { | |
public static void main(String[] args) throws Exception { | |
CharStream input = new ANTLRFileStream(args[0]); | |
String templateFile; | |
if ( args.length > 1 ) { | |
templateFile = args[1]; | |
} else { | |
templateFile = "PythonWrapper.stg"; | |
} | |
String output = WrapperGenV4.generate( input, templateFile ); | |
System.out.println( output ); | |
} | |
} |
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
import org.antlr.v4.runtime.*; | |
import org.antlr.v4.runtime.tree.*; | |
import org.antlr.stringtemplate.*; | |
import java.io.FileReader; | |
public class WrapperGenV4 { | |
public static String generate( String input, String templateFile ) | |
throws Exception { | |
CharStream is = new ANTLRInputStream( input ); | |
return generate( is, templateFile ); | |
} | |
public static String generate( String input ) | |
throws Exception { | |
CharStream is = new ANTLRInputStream( input ); | |
return generate( is ); | |
} | |
public static String generate( CharStream input ) throws Exception { | |
return generate( input, "PythonWrapper.stg"); | |
} | |
public static String generate( CharStream input, String templateFile ) throws Exception { | |
CFunctionLexer lex = new CFunctionLexer(input); | |
CommonTokenStream tokens = new CommonTokenStream(lex); | |
CFunctionParser parser = new CFunctionParser(tokens); | |
ParseTree tree = parser.function(); | |
FileReader groupFileR = new FileReader( templateFile ); | |
StringTemplateGroup templates = new StringTemplateGroup( groupFileR ); | |
groupFileR.close(); | |
StringTemplate t = templates.getInstanceOf( "function" ); | |
CFunctionWalker listener = new CFunctionWalker( t ); | |
ParseTreeWalker walker = new ParseTreeWalker( ); | |
walker.walk( listener, tree ); | |
return listener.out; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment