Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>but outside of a situation like that, &lt;% would always indicate the start of a code block.</p> </blockquote> <p>In that case, first scan the file for your embedded code, and once you have those, parse your embedded code with a dedicated parser (without the noise before the <code>&lt;%</code> and after the <code>%&gt;</code> tags).</p> <p>ANTLR has the option to let the lexer parse just a (small) part of an input file and ignore the rest. Note that you cannot create a "combined grammar" (parser and lexer in one) in that case. Here's how you can create such a "partial lexer":</p> <pre><code>// file EmbeddedCodeLexer.g lexer grammar EmbeddedCodeLexer; options{filter=true;} // &lt;- enables the partial lexing! EmbeddedCode : '&lt;%' // match an open tag ( String // ( match a string literal | ~('%' | '\'') // OR match any char except `%` and `'` | {input.LT(2) != '&gt;'}?=&gt; '%' // OR only match a `%` if `&gt;` is not ahead of it )* // ) &lt;- zero or more times '%&gt;' // match a close tag ; fragment String : '\'' ('\\' . | ~('\'' | '\\'))* '\'' ; </code></pre> <p>If you now create a lexer from it:</p> <pre><code>java -cp antlr-3.2.jar org.antlr.Tool EmbeddedCodeLexer.g </code></pre> <p>and create a little test harness:</p> <pre><code>import org.antlr.runtime.*; public class Main { public static void main(String[] args) throws Exception { String source = "Lorem ipsum dolor sit amet \n"+ "&lt;% \n"+ "a = 2 &gt; 1 &amp;&amp; 10 % 3; \n"+ "print('consectetur %&gt; adipiscing elit'); \n"+ "%&gt; \n"+ "Phasellus volutpat dignissim sapien. \n"+ "foo &lt;% more code! %&gt; bar \n"; ANTLRStringStream in = new ANTLRStringStream(source); EmbeddedCodeLexer lexer = new EmbeddedCodeLexer(in); CommonTokenStream tokens = new CommonTokenStream(lexer); for(Object o : tokens.getTokens()) { System.out.println("=======================================\n"+ "EmbeddedCode = "+((Token)o).getText()); } } } </code></pre> <p>compile it all:</p> <pre><code>javac -cp antlr-3.2.jar *.java </code></pre> <p>and finally run the Main class by doing:</p> <pre><code>// *nix/MacOS java -cp .:antlr-3.2.jar Main // Windows java -cp .;antlr-3.2.jar Main </code></pre> <p>it will produce the following output:</p> <pre><code>======================================= EmbeddedCode = &lt;% a = 2 &gt; 1 &amp;&amp; 10 % 3; print('consectetur %&gt; adipiscing elit'); %&gt; ======================================= EmbeddedCode = &lt;% more code! %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload