User Tools

Site Tools


scan_statement

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
scan_statement [2020/09/24 12:25]
revusky [The SCAN construct also allows the newer LOOKBEHIND construct.]
scan_statement [2021/02/08 18:16] (current)
revusky
Line 1: Line 1:
 ===== The new SCAN Statement ===== ===== The new SCAN Statement =====
  
-JavaCC 21 introduces a new ''SCAN'' statement that is designed to supersede the legacy ''LOOKAHEAD''. It is a key part of the [[new_syntax_summary|newer streamlined syntax]]. +JavaCC 21 introduces a new ''SCAN'' statement that is designed to supersede the legacy ''LOOKAHEAD''. It is a key part of the [[new_syntax_summary|newer streamlined syntax]]. Like ''LOOKAHEAD''''SCAN'' exists to specify conditions at [[choice points]] that override the default resolution mechanism of scanning ahead exactly one token. The new ''SCAN'' construct provides a superset of the functionality of ''LOOKAHEAD'' and tends to more succinct and readable. (Sometimes dramatically so.)
- +
-Note, however, that all the older syntax still works and will work for the foreseeable feature+
  
 +Note, however, that the older ''LOOKAHEAD'' syntax still works and will work for the foreseeable feature. 
 Here is a rundown of the differences between ''SCAN'' and the older ''LOOKAHEAD''. Here is a rundown of the differences between ''SCAN'' and the older ''LOOKAHEAD''.
  
Line 24: Line 23:
  
 <code> <code>
-LOOKAHEAD(Foo()) Foo() Bar()+LOOKAHEAD("foo" ("bar"|"baz")) FooBar()
 </code> </code>
  
Line 30: Line 29:
  
 <code> <code>
-SCAN Foo => Foo Bar+SCAN "foo" ("bar"|"baz"=> FooBar
 </code> </code>
  
Line 94: Line 93:
  
  
-In the legacy lookahead, if you only have semantic lookahead, the number of tokens to be scanned is assumed to be zero, i.e. if the condition is true, you automatically go into the following expansion.+In the legacy lookahead, if you only have semantic lookahead, the number of tokens to be scanned is assumed to be zero, i.e. if the condition is true, you automatically go into the following expansion. JavaCC 21 makes the opposite assumption in these spots. Unless you specify otherwise, we assume //unlimited// lookahead.
  
  
-==== The SCAN construct also allows the newer LOOKBEHIND construct. ====+==== The SCAN construct also you to use contextual predicates. ====
  
-This is [[LOOKBEHIND|outlined separately]].+The [[contextual_predicates]] construct allows you to express conditions based on scanning backwards in the lookahead/call stack. This can only be used in conjunction with the newer ''SCAN'' statement, not with the legacy ''LOOKAHEAD'' construct.  
 + 
 +You can find more information [[contextual_predicates|here]].
  
 ==== Addendum: the SCAN-less SCAN? ==== ==== Addendum: the SCAN-less SCAN? ====
Line 158: Line 159:
 </code> </code>
  
-In this case, because I think it is so common, we decided to allow this. And, in fact, you can see that this is already used in internal development, for example [[https://github.com/javacc21/javacc21/blob/master/src/main/grammars/JavaCC.javacc#L1028|here]].+In this case, because I think it is so common, we decided to allow this. And, in fact, you can see that this is already used in internal development, for example [[https://github.com/javacc21/javacc21/blob/master/src/main/grammars/JavaCC.javacc#L1950|here]]
 + 
 + 
 +==== Recap ==== 
 + 
 +The new ''SCAN'' instruction in JavaCC 21 totally supersedes the legacy tool's ''LOOKAHEAD''. It allows you to specify in the following order: 
 +  * //numerical// lookahead, i.e. the maximum number of tokens to scan ahead 
 +  * [[semantic lookahead]], i.e. arbitrary java code enclosed in {..} 
 +  * a [[contextual_predicates]] predicate 
 +  * [[syntactic lookahead]] 
 + 
 +Caveats:  
 +  * The ''SCAN'' construct can specify all (or none) of the above, except that //numerical// lookahead and [[syntactic lookahead]] are mutually exclusive.  
 +  * If no numerical or syntactic lookahead is specified, the generated code will scan ahead an //unlimited// number of tokens. This differs from the legacy tool. 
 + 
 +Note that an empty SCAN statement can be simply written alternatively with a lone arrow, i.e. ''=>''.
  
-===== The newer SCAN construct allows you to use the newer "lookbehind" construct  
  
-This is documented separately. 
  
scan_statement.1600950325.txt.gz · Last modified: 2020/09/24 12:25 by revusky