grappa.extension

AST interpreter extension for parboiled/grappa parser.

View the Project on GitHub uscexp/grappa.extension

grappa.extension

Introduction

AST interpreter extension for parboiled/grappa parser.

News

22.Sep.2014: New release v0.3.0 which adds an PEG parser java source code generator from a given PEG file. Still beta, but I deleted beta tag from version.

21.Aug.2014: New release v0.2.0-Beta which adds an AST commands java source code generator.

15.Aug.2014: First release v0.1.0-Beta which adds the AST interpreter.

Features

Description

The AstInterpreter interpretes a PEG defined language based on a grappa BaseParser. First it creates an AST (Abstract Syntax Tree) based on the grappa ParsingResult and the annotated parser methods, annotated with @AstValue or @AstCommand. @AstValue writes the parse node value to the interpreter stack. By default the interpreter stores the values as in the parser defined type, if one wants to change that, one has to define the corresponding parameters of the @AstValue annotation. @AstCommand must be implemented from a developer, which has to create the Ast<Name>TreeNode and implement it. This represents the logic of your language commands. The AstCommandTreeNode classes by default are located in the same package as the parser implementation and have the naming convetion of Ast + name of your parser rule (first char upper case) + TreeNode and must extend AstCommandTreeNode. If one want to change that, one has to define the corresponding parameters of the @AstCommand annotation.

The AstModelGenerator creates the model classes from a grappa BaseParser extended and annotated parser class. It creates the corresponding java source files for the @AstCommand annotated rules in the parser class. The generated classes will extend AstCommandTreeNode and the to be implemented method interpret.

The PegParserGenerator generates a BaseParser extended parser java class file, from a given Parser Expression Grammar (PEG) file. The allowed PEG syntax is based on (Bryan Ford's) definitions.

Example

...
String input = "2 * 2 + 2 * 3";
CalculatorParser calculatorParser = Parboiled.createParser(CalculatorParser.class);

RecoveringParseRunner<CalculatorParser> recoveringParseRunner = new RecoveringParseRunner<>(calculatorParser.inputLine());

ParsingResult<CalculatorParser> parsingResult = recoveringParseRunner.run(input);

Node<CalculatorParser> root = parsingResult.parseTreeRoot;
AstInterpreter<Double> interpreter = new AstInterpreter<>();
Long id = new Date().getTime();
interpreter.execute(calculatorParser.getClass(), parsingResult, id);
interpreter.cleanUp(id);
...

First it creates an instance of CalculatorParser with the grappa factory method. Then one need a ParseRunner and run it with the input string. This gives us the ParsingResult which we need for the parse tree root. Up to now we have only treated with the parboiled/grappa parser. Now to the point. We create an AstInterpreter and call the execute method with the parameters calculation parser class, parsing result and an id. This will create the AST structure and interpret it. Finally clean up the interpreter (remove process store like stack ...).

For the java source code model generation use: java -cp <dependencies> com.github.uscexp.grappa.extension.codegenerator.AstModelGenerator pasrserClass sourceOutputPath.

e.g. java -cp grappa.jar;asm-debug-all.jar;guava.jar;codemodel.jar;grappa.extension.jar;MyParserLib.jar; com.github.uscexp.grappa.extension.codegenerator.AstModelGenerator com.github.uscexp.grappa.parser.CalculatorParser src/main/java

For the PEG parser java source code generation use: java -cp <dependencies> com.github.uscexp.grappa.extension.codegenerator.PegParserGenerator parserClassString genericTypeName sourceOutputPath pegFileInputPath <inputFileEncoding>.

e.g. java -cp grappa.jar;asm-debug-all.jar;guava.jar;codemodel.jar;grappa.extension.jar;MyParserLib.jar; com.github.uscexp.grappa.extension.codegenerator.PegParserGenerator com.github.uscexp.grappa.parser.GeneratedParser java.lang.String target PEGSyntax.peg

Default character encoding is UTF-8.

Installation

To use the library in your java project just put the binary jar file to your classpath. The maven dependency can be found here: mvnrepository

(C) 2014 by haui