Skip to content

String token matching is happening after normal token matching #40

Description

@Angus-Warman

A brief example

const (
	TMath = iota + 1
	TComment
)

func main() {
	lexer := tokenizer.New()
	lexer.DefineStringToken(TComment, `//`, "\n")
	lexer.DefineTokens(TMath, []string{"+", "-", "/", "*"})

	sample := `
	1 + 2
	// Comment
	10 / 2
	`

	stream := lexer.ParseString(sample)
	defer stream.Close()

	for stream.IsValid() {
		t := stream.CurrentToken()
		fmt.Printf("key=%v value=%v\n", t.Key(), t.ValueString())
		stream.GoNext()
	}
}

Result:

key=-2   value="1"
key=1    value="+"
key=-2   value="2"
key=1    value="/"
key=1    value="/"
key=-1   value="Comment"
key=-2   value="10"
key=1    value="/"
key=-2   value="2"

I would've expected the result to include:

key=0 value="// Comment"

Is this a bug? If so, I'll have a look into creating a PR. The workaround in my case was to use python-style comments (#) instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions