meta data for this page
  •  

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
code_injection_in_javacc_21 [2020/02/09 17:01] – [2. The INJECT block with no type specified] revuskycode_injection_in_javacc_21 [2020/09/29 10:24] (current) – [1. An INJECT block that specifies the class name] revusky
Line 2: Line 2:
  
  
-Note, first of all, that he ''INJECT'' statement does not exist in [[https://javacc.github.io/javacc/|Legacy JavaCC]]. +Note, first of all, that the ''INJECT'' statement does not exist in [[https://javacc.github.io/javacc/|Legacy JavaCC]]. 
  
 ''INJECT'' allows you to specify extra code that will be placed (or "injected") in generated java code. It can appear anywhere in your grammar where you could place a BNF production or Token production. It can be written in one of two forms. ''INJECT'' allows you to specify extra code that will be placed (or "injected") in generated java code. It can appear anywhere in your grammar where you could place a BNF production or Token production. It can be written in one of two forms.
Line 10: Line 10:
 This form a class (or interface) name and looks something like this: This form a class (or interface) name and looks something like this:
  
-   INJECT(Foobar +   INJECT Foobar :  
-    +      import foo.bar.IFoobar
-      import foo.bar.Foobar+
       implements IFoobar;       implements IFoobar;
-    
        
        private Foo value;        private Foo value;
Line 23: Line 21:
 The above snippet should be self-explanatory. The INJECT statement specifies the class in which to inject the code. In this example, it is ''Foobar''. The first block after the colon is a prologue in which you can specify imports as well as en extends and/or an implements clause. The second block contains the actual code to be injected in the type declaration itself. The above snippet should be self-explanatory. The INJECT statement specifies the class in which to inject the code. In this example, it is ''Foobar''. The first block after the colon is a prologue in which you can specify imports as well as en extends and/or an implements clause. The second block contains the actual code to be injected in the type declaration itself.
  
-Since an INJECT block can be placed anywhere in your grammar file where you could put a production, you would naturally tend to place the above snippet next to the relevant BNF production, which would, of course, be the ''Foobar()'' production. +Since an INJECT block can be placed anywhere in your grammar file where you could put a production, you would naturally tend to place the above snippet next to the relevant BNF production, which would, of course, be the ''Foobar'' production. 
  
 ===== 2. The INJECT block with no type specified ===== ===== 2. The INJECT block with no type specified =====
Line 29: Line 27:
  
 The other form of writing ''INJECT'' does not specify the class into which the code will be injected. That looks something like this: The other form of writing ''INJECT'' does not specify the class into which the code will be injected. That looks something like this:
 +<html><pre>
    INJECT : {    INJECT : {
      import foo.bar.Edible;      import foo.bar.Edible;
Line 41: Line 39:
      }      }
    }    }
 +</pre></html>   
        
 The above code causes the code within the block to be injected inside the generated Token.java file -- in this example, it would be presumably so that the Token object can implement the ''foo.bar.Edible'' interface. The above code causes the code within the block to be injected inside the generated Token.java file -- in this example, it would be presumably so that the Token object can implement the ''foo.bar.Edible'' interface.