blynn / nex

Lexer for Go
http://cs.stanford.edu/~blynn/nex/
GNU General Public License v3.0
416 stars 47 forks source link

Multiline Comments #37

Open bantl23 opened 8 years ago

bantl23 commented 8 years ago

Are multiline comments possible with nex? I've been playing with nex quite a bit and I'm very impressed. However, I'm having trouble figuring out how to handle multiline comments. Can this been done with the nesting feature? I've tried but could not figure it out. Below is one thing I tired but I regex is greedy the nesting grabs the rest of the document.

/[0-9]+/            { println("INTEGER:", txt()) }
/[0-9]+\.[0-9]*/    { println("FLOAT:", txt()) }
/if|then|begin|end|procedure|function/
                    { println( "KEYWORD:", txt()) }
/[a-z][a-z0-9]*/    { println( "ID:", txt()) }
/\+|-|\*|\//        { println("OP:", txt()) }
/[ \t]+/            { /* eat up whitespace */ }
/\/\*.*|\n*\*\// <  { println("BEG_MULTI:", txt()) }
  /.*|\n*\*\//      { println("BEG_COMMENT:", txt()) }
  /\n/              { println("NEWLINE") }
>                   { println("END_COMMENT", txt()) }
/\n/                { println("NEWLINE") }
/./                 { println("UNRECOGNIZED CHAR:", txt()) }
//
package main
import (
  "os"
)

func main() {
  lex := NewLexer(os.Stdin)
  txt := func() string { return lex.Text() }
  NN_FUN(lex)
}
RSDuck commented 7 years ago

This question is a little bit old, but I encountered the same problem and I found three possible solutions:

  1. Match /* and then mess with nex internals so that it skips everything until the next */ (or when an counter reaches 0 so that recursion is possible). I didn't tried if this works and I wouln't recommend trying it.
  2. Match against /* and */ and increase or decrease a variable(or just set it to true or false, if you don't wan't recursive comments). And in all other matches add a condition which checks if the match is within a comment, so that it gets skipped.
  3. If you create a special reader, which wraps around another one you can filter all comments before they get lexed. This is the best solution I came up with but I didn't tried it.
purpleidea commented 7 years ago

FWIW, here's some info on single line comment matching: https://github.com/blynn/nex/issues/45