tokenizer icon indicating copy to clipboard operation
tokenizer copied to clipboard

Parsing comments

Open ddorstijn opened this issue 1 year ago • 3 comments

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

ddorstijn avatar Sep 25 '24 06:09 ddorstijn

Can you provide an example what you want to do?

bzick avatar Sep 25 '24 17:09 bzick

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;

ddorstijn avatar Sep 27 '24 15:09 ddorstijn

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())
}

bzick avatar Sep 28 '24 15:09 bzick