Skip to content

Instantly share code, notes, and snippets.

@miho
Created June 8, 2018 11:54
Show Gist options
  • Save miho/dc6193f3b11c78f5ed320fa4130b1dd7 to your computer and use it in GitHub Desktop.
Save miho/dc6193f3b11c78f5ed320fa4130b1dd7 to your computer and use it in GitHub Desktop.
grammar org.xtext.example.mydsl.take1.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/take1/MyDsl"
Json: value=Val;
Obj:
'{'
pairs+=Pair (',' pairs+=Pair)*
'}'
|
'{' '}'
;
Pair:
key=STRING ':' value=Val;
Val: ObjectValue | StringValue | ArrayValue | BooleanValue | NullValue | NumberValue;
Array:
'[' values+=Val (',' values+=Val)* ']'
|
'[' ']'
;
StringValue:
value=STRING;
ObjectValue:
value=Obj;
BooleanValue:
value='true' | value='false';
NullValue:
'null';
NumberValue:
value=Number;
ArrayValue:
value=Array;
terminal Number:
'-'? INT ('.' INT)? (('E'|'e') '-'? INT)?;
// Setup and serializer
this.injector = new MyDslStandaloneSetup().createInjectorAndDoEMFRegistration();
this.serializer = injector.getInstance(Serializer.class);
// parsing is done via
IParser parser = injector.getInstance(IParser.class);
IParseResult result = parser.parse(new StringReader(input));
// serialization is done via
String code = serializer.serialize(object);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment