Skip to content

Commit

Permalink
fix: properly lex imports in OPL (#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Sep 30, 2022
1 parent 7aacf0d commit 26944e9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
"'import'",
"{",
"'Foo'",
",",
"'Bar'",
",",
"'Baz'",
"}",
"'from'",
"'@ory/keto-...'",
"EOF"
]
13 changes: 10 additions & 3 deletions internal/schema/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion internal/schema/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -117,7 +119,6 @@ func TestLexer(t *testing.T) {
}
if item.Typ == itemEOF {
t.Fatal("reached EOF, but expected error")
break
}
}
l.next()
Expand Down
2 changes: 2 additions & 0 deletions internal/schema/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down

0 comments on commit 26944e9

Please sign in to comment.