From 26944e9e2f5d851aec63e46b3777d2d1ebaca3f8 Mon Sep 17 00:00:00 2001 From: Henning Perl Date: Fri, 30 Sep 2022 14:08:41 +0200 Subject: [PATCH] fix: properly lex imports in OPL (#1041) --- .../TestLexer-suite=snapshots-import_statement.json | 13 +++++++++++++ internal/schema/lexer.go | 13 ++++++++++--- internal/schema/lexer_test.go | 3 ++- internal/schema/parser_test.go | 2 ++ 4 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 internal/schema/.snapshots/TestLexer-suite=snapshots-import_statement.json diff --git a/internal/schema/.snapshots/TestLexer-suite=snapshots-import_statement.json b/internal/schema/.snapshots/TestLexer-suite=snapshots-import_statement.json new file mode 100644 index 000000000..4d561cf29 --- /dev/null +++ b/internal/schema/.snapshots/TestLexer-suite=snapshots-import_statement.json @@ -0,0 +1,13 @@ +[ + "'import'", + "{", + "'Foo'", + ",", + "'Bar'", + ",", + "'Baz'", + "}", + "'from'", + "'@ory/keto-...'", + "EOF" +] diff --git a/internal/schema/lexer.go b/internal/schema/lexer.go index 3b55edcc5..afbb36169 100644 --- a/internal/schema/lexer.go +++ b/internal/schema/lexer.go @@ -307,9 +307,16 @@ func lexBlockComment(l *lexer) stateFn { func lexStringLiteral(l *lexer) stateFn { r := l.next() l.ignore() - l.acceptRun(digits + letters) - if l.peek() != r { - return l.errorf("unclosed string literal") + +loop: + for { + switch l.next() { + case eof: + return l.errorf("unclosed string literal") + case r: + l.backup() + break loop + } } l.emit(itemStringLiteral) l.next() diff --git a/internal/schema/lexer_test.go b/internal/schema/lexer_test.go index f9955986b..0ac5e7a43 100644 --- a/internal/schema/lexer_test.go +++ b/internal/schema/lexer_test.go @@ -11,10 +11,12 @@ var lexingErrorTestCases = []struct{ name, input string }{ {"open comment", "/*"}, {"open string literal", "'"}, {"non-token", "ΓΌ"}, + {"mixed quotes", `"invalid_literal'`}, } var lexableTestCases = []struct{ name, input string }{ {"empty", ""}, + {"import statement", "import { Foo, Bar, Baz } from '@ory/keto-namespace-types'"}, {"single class", ` class name implements Namespace { metadata = { @@ -117,7 +119,6 @@ func TestLexer(t *testing.T) { } if item.Typ == itemEOF { t.Fatal("reached EOF, but expected error") - break } } l.next() diff --git a/internal/schema/parser_test.go b/internal/schema/parser_test.go index 781d48edc..6e282aaf2 100644 --- a/internal/schema/parser_test.go +++ b/internal/schema/parser_test.go @@ -17,6 +17,8 @@ var parserTestCases = []struct { name, input string }{ {"full example", ` + import { Namespace, SubjectSet, FooBar, Anything } from '@ory/keto-namespace-types' + class User implements Namespace { related: { manager: User[]