bzick / tokenizer

Tokenizer (lexer) for golang
MIT License
98 stars 6 forks source link

Parsing comments #21

Open ddorstijn opened 1 month ago

ddorstijn commented 1 month ago

It is currently possible not possible to parse comments. A work around might be for block comments to parse it as a string token. But inline comments need to know the end of the line, which is not possible as far as I am aware

bzick commented 1 month ago

Can you provide an example what you want to do?

ddorstijn commented 1 month ago

I think every language with an inline comment could be an example. Take SQL:

select 1 + 1 -- This returns 2
from xyz

Javascript

const x = a + b; // This is an inline comment
myFunc();

Block comments would be something like this:

/* This query returns the result of 1 + 1
    Written by xyz
*/
select 1 + 1

And javascript

/* Return a + b */ return a + b;
bzick commented 1 month ago

You can parse inline comment like block comment (via string token) using \n as end of string:

func TestIssue21(t *testing.T) {
    parser := New()
    var InlineCommentToken TokenKey = 1
    parser.DefineStringToken(InlineCommentToken, "--", "\n")

    stream := parser.ParseString(`one -- 123 comment
two`)

    require.Equal(t, "one", stream.CurrentToken().ValueString())
    stream.GoNext()
    require.Equal(t, InlineCommentToken, stream.CurrentToken().StringKey())
    require.Equal(t, "-- 123 comment\n", stream.CurrentToken().ValueString())
    stream.GoNext()
    require.Equal(t, "two", stream.CurrentToken().ValueString())
}