grammar - Capturing content which can start with Parser keywords in Xtext -


the following simplified version of actual grammar :-

grammar org.hello.world   import "http://www.eclipse.org/emf/2002/ecore" ecore  generate world "http://www.hello.org/world"  model:     content=anycontent greetings+=greeting*;  anycontent:     (id | any_other)* ;  greeting:     '<hello>' name=id '</hello>';  terminal id:     ('a'..'z'|'a'..'z')+ ;  terminal any_other:     . ; 

so using above grammar if input :-

<hi><hello>world</hello>

then getting syntax error saying mismatched character 'i' expecting 'e' @ column 2 .

my requirement anycontent should match "<hi>" , can guide me how achieve that?

if want make xtext. advice split problem. first problem syntaxic, need parser file. second problem semantic, want give "sense" objets , tell container. define container , containment xml can't done inside grammar.

  1. make custom ecore , make easy grammar, start , end tag. don't care name of tag. example :

    model returns xmlfile: (starttag|endtag|text)+;  text returns text: text=string;  starttag returns starttag: '<' name=id '>';  endtag returns endtag: '</' name=id '>'; 
  2. change tokensource. token source give token parser. can override nature of token, merge or split them. idea here merge token outside between of ">" , "</". token represent text, can create single token elements containing between elements. example :

    class customtokensource extends xtexttokenstream{   new(tokensource tokensource, itokendefprovider tokendefprovider) {       super(tokensource,tokendefprovider) }  override lt(int k) {     var token token = super.lt(k)     if(token != null && token.text != null) token.tokenoverride(k);     token } 

in example need add custom code on method "tokenoverride".

add custom token source on parser :

class xdslparser extends dslparser{  override protected xtexttokenstream createtokenstream(tokensource tokensource) {    return new customtokensource(tokensource, gettokendefprovider()); }  } 
  1. compute containement : containment of elements can compute after parsing. after it, can model , change will. make it, need override method "doparse" of parser "xdslparser" follow :

    override protected iparseresult doparse(string rulename, charstream in,         nodemodelbuilder nodemodelbuilder, int initiallookahead) {       var iparseresult result = super.doparse( rulename,  in,  nodemodelbuilder,  initiallookahead)  //give model         result.rootastelement;      return result 

    }

note : model obtain after parsing flat. xmlfile object contain elements in order. need write algorithm build containement on ast model.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -