Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Latest commit

 

History

History
61 lines (48 loc) · 1.59 KB

FileGenerator.md

File metadata and controls

61 lines (48 loc) · 1.59 KB

JainParse Code Generator

This generator generates the Source files to easy read a parsed Grammar.

Usage

As you have a valid Grammar; for example:

Grammar string = Grammar.build("StringGrammar")
.matchToken(Tokens.STRING_DELIMITTER) // No name as we dont care about the delmitters
.anyExcept(Tokens.STRING_DELIMITTER,"value") //Named property to get it later on
.matchToken(Tokens.STRING_DELIMITTER); // No name as we dont care here to

Then you simply pass it into the Generator

JPGenerator gen = new JPGenerator();
gen.setOutputDirecory("C:\\Users\\Example\\Workspace\\Project\\src");
gen.setPackage("com.niton.generated");
gen.generate(string);

Result

//removed imports as they are not neccesary here
public class StringGrammar /*The name of the grammar*/ {
	private SubGrammarObject obj;
	public StringGrammar(SubGrammarObject obj){
		this.obj = obj;
	}
	public String getValue(){
		//some code you do not need to know
	}
}

So you can simply parse and use like this

//Building grammar
Grammar string = Grammar.build("StringGrammar")
.matchToken(Tokens.STRING_DELIMITTER)
.anyExcept(Tokens.STRING_DELIMITTER,"value")
.matchToken(Tokens.STRING_DELIMITTER);

//Parse into object
Parser p = new Parser(string);
SubGrammarObject obj = p.parse("\"Ich bin ein String\"");

//Read with generated Code (obviusly you need to generate it first)
StringGrammar parsed = new StringGrammar(obj);
System.out.println(parsed.getValue()); //output: Ich bin ein String

Written with StackEdit.