blynn / nex

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

Unmatched characters are skipped! #42

Closed purpleidea closed 6 years ago

purpleidea commented 7 years ago

hey there,

playing with nex, and using the below... The problem is I had to add an ERROR identifier to match anything not previously caught since otherwise random typos in the lexed code are skipped silently. Any advice on how to improve this?

Thanks!

/[ \t\n]/   { /* Skip blanks and tabs. */ }
/{/     { return OPEN_CURLY }
/}/     { return CLOSE_CURLY }
/if/        { return IF }
/else/      { return ELSE }
/=/     { return EQUALS }
/\$[a-z]+/  {
            s := yylex.Text()
            lval.str = s[1:len(s)] // remove the leading $
            return VAR_IDENTIFIER
        }
/[a-z]+/    {
            lval.str = yylex.Text()
            return IDENTIFIER
        }
/./     { return ERROR }
//
package main
import ()
blynn commented 6 years ago

https://westes.github.io/flex/manual/Matching.html#Matching

Flex, which inspired nex, gives unmatched characters a default "ECHO" action, which means they get printed to standard output. Should I make nex do this too?

purpleidea commented 6 years ago

That's a good question, and I'm not sufficiently enough of an expert to know what the right decision is. My only guess is that it would be less useful to print to stdout because putting stuff on the terminal of the calling program wouldn't be great. If anything it should be optional.

At least for now, I've dealt with the ERROR case, and it's not the worst thing in the world for me. I'm not blocked on this, so I guess I can close it.