Note that there are some explanatory texts on larger screens.

plurals
  1. POsemicolon insertion ala google go with flex
    text
    copied!<p>I'm interested in adding semi-colon insertion ala Google Go to my flex file.</p> <p>From the Go documentation:</p> <blockquote> <h1>Semicolons</h1> <p>Like C, Go's formal grammar uses semicolons to terminate statements; unlike C, those semicolons do not appear in the source. Instead the lexer uses a simple rule to insert semicolons automatically as it scans, so the input text is mostly free of them.</p> <p>The rule is this. If the last token before a newline is an identifier (which includes words like int and float64), a basic literal such as a number or string constant, or one of the tokens</p> <pre><code>break continue fallthrough return ++ -- ) } </code></pre> <p>the lexer always inserts a semicolon after the token. This could be summarized as, “if the newline comes after a token that could end a statement, insert a semicolon”.</p> <p>A semicolon can also be omitted immediately before a closing brace, so a statement such as</p> <pre><code>go func() { for { dst &lt;- &lt;-src } }() </code></pre> <p>needs no semicolons. Idiomatic Go programs have semicolons only in places such as for loop clauses, to separate the initializer, condition, and continuation elements. They are also necessary to separate multiple statements on a line, should you write code that way.</p> <p>One caveat. You should never put the opening brace of a control structure (if, for, switch, or select) on the next line. If you do, a semicolon will be inserted before the brace, which could cause unwanted effects. Write them like this</p> <pre><code>if i &lt; f() { g() } </code></pre> <p>not like this</p> <pre><code>if i &lt; f() // wrong! { // wrong! g() // wrong! } // wrong! </code></pre> </blockquote> <p>How would I go about doing this (how can I insert tokens in the stream, how can I see the last token that was matched to see if it is a good idea, etc etc etc)?</p> <p>I am using bison too, but Go seems to just use their lexer for semicolon insertion.</p>
 

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