Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » accepting php html in xtext grammar
accepting php html in xtext grammar [message #777569] Tue, 10 January 2012 18:09 Go to next message
Ivo Galic is currently offline Ivo GalicFriend
Messages: 1
Registered: January 2012
Junior Member
Hi ppl, I have grammar that should accept the PHP and HTML code (ignore it) and parse some other code but i cant get it work. As you can see I managed to accept PHP , but HTML is giving me a headache, because there is no beginning and closing tags. I know HTML can be between these 3 tags (Single_fn | Double_fn | PHP ). How could I define a grammar for this?
Thx in advance
Ivo

Grammar(part):
Npf returns Npf hidden(WS, ML_COMMENT, SL_COMMENT):
	layout+= Function*
	;

Function returns Function:
	{Function}
	fn =  Single_fn | Double_fn | PHP 
	;



PHP returns PHP:
	{PHP}
	 BETWEEN_PHP 
;

terminal BETWEEN_PHP    : '<?php' .* '?>';

terminal BETWEEN_HTML : ' ' -> ' ';

HTML returns HTML:
	{HTML}
	BETWEEN_HTML
;

Single_fn returns Single_fn:
	s1=  '<#npf_' fn2=SingleFunctionName g2='#>'
;


Double_fn returns Double_fn:
	gg ='<#npf_db #>' 
;

Re: accepting php html in xtext grammar [message #777611 is a reply to message #777569] Tue, 10 January 2012 20:04 Go to previous message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
The thing you have to understand first and foremost for this is that the input (i.e., the DSL file) is first lexed before it is parsed. Lexing is a pretty straightforward process and lexing cannot be influenced by the parsing, so the lexing happens completely oblivious of any context: in this case, whether you are inside a PHP block or in HTML context. It is usually tricky to get good (maintainable/expandable) grammars for these kinds of "mixed context" languages, typically depending on the number and nature of keywords in the contexts. Often, it is easiest to write a custom lexer that is -to some extent- context-aware. I've done that for SGML/XML: see my GitHub repo - all the projects with sgml, dtd or simplemarkup in their names.

In your case, it's important to know that the terminal BETWEEN_HTML is not called but rather matched from the HTML rule. (Also, the "X returns X:" is superfluous in this situation: "X:" will do fine.)



Previous Topic:HTML doc for web and content assist ?
Next Topic:[xtend2] java enums
Goto Forum:
  


Current Time: Thu Apr 25 15:30:19 GMT 2024

Powered by FUDForum. Page generated in 0.03105 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top