Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline Comments #37

Open
bantl23 opened this issue Feb 25, 2016 · 2 comments
Open

Multiline Comments #37

bantl23 opened this issue Feb 25, 2016 · 2 comments

Comments

@bantl23
Copy link

bantl23 commented Feb 25, 2016

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
Copy link

RSDuck commented Sep 25, 2016

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
Copy link
Collaborator

FWIW, here's some info on single line comment matching: #45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants