Created
April 21, 2013 12:25
-
-
Save mattmcd/5429430 to your computer and use it in GitHub Desktop.
ANTLR3 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; | |
options { | |
output = AST; | |
ASTLabelType = CommonTree; | |
} | |
tokens { | |
SCALAR; | |
ARRAY; | |
ARRAYLEN; | |
INARGS; | |
FUNCTION; | |
RETTYPE; | |
UNIT; | |
} | |
function : retType name args -> ^(FUNCTION name ^(RETTYPE retType) ^(INARGS args)); | |
args : '(' ! arg (','! arg)* ')' !; | |
arg : 'double' name -> ^(SCALAR name) | |
| 'double' '*' name -> ^(ARRAY name) | |
| 'int' name -> ^(ARRAYLEN name); | |
retType : 'double' -> SCALAR | |
| 'double' '*' -> ARRAY | |
; | |
name : ID; | |
ID : LETTER WORD*; | |
fragment | |
LETTER : 'a'..'z'|'A'..'Z'; | |
fragment | |
DIGIT : '0'..'9'; | |
fragment | |
WORD : LETTER | DIGIT | '_'; | |
WS : (' '|'\r'|'\t'|'\n')+ { $channel = HIDDEN; }; |
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.runtime.*; | |
import java.io.FileReader; | |
public class Main { | |
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 = WrapperGen.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.runtime.*; | |
import org.antlr.runtime.tree.*; | |
import org.antlr.stringtemplate.*; | |
import java.io.FileReader; | |
public class WrapperGen { | |
public static String generate( String input, String templateFile ) | |
throws Exception { | |
CharStream is = new ANTLRStringStream( input ); | |
return generate( is, templateFile ); | |
} | |
public static String generate( String input ) | |
throws Exception { | |
CharStream is = new ANTLRStringStream( 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); | |
// System.out.println("tokens="+tokens); | |
CFunctionParser parser = new CFunctionParser(tokens); | |
CFunctionParser.function_return r = parser.function(); | |
CommonTree t = (CommonTree) r.getTree(); | |
CommonTreeNodeStream nodes = new CommonTreeNodeStream( t ); | |
nodes.setTokenStream( tokens ); | |
FileReader groupFileR = new FileReader( templateFile ); | |
StringTemplateGroup templates = new StringTemplateGroup( groupFileR ); | |
groupFileR.close(); | |
WrapperWalker walker = new WrapperWalker( nodes ); | |
walker.setTemplateLib( templates ); | |
WrapperWalker.function_return ret = walker.function(); | |
return ret.getTemplate().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
tree grammar WrapperWalker; | |
options { | |
tokenVocab = CFunction; | |
ASTLabelType = CommonTree; | |
output = template; | |
} | |
function : ^(FUNCTION (n=name) ^(RETTYPE (retType)) | |
^(INARGS ((s+=scalar)* ((ar+=array)+ l=len)? ) ) ) | |
->function(name={$n.st}, | |
scalars={$s}, arrays={$ar}, len={$l.st} ); | |
// args : (a+=arg)* -> { $a }; | |
scalar : ^(SCALAR name) -> {$name.st}; | |
array : ^(ARRAY name) -> {$name.st}; | |
len : ^(ARRAYLEN name) -> {$name.st}; | |
retType :SCALAR | |
|ARRAY | |
; | |
name : ID -> {%{$ID.text}}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment