-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Port in expiration parsing into composable schema DSL #2239
Merged
+563
−81
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 23 additions & 21 deletions
44
pkg/composableschemadsl/dslshape/zz_generated.nodetype_string.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package lexer | ||
|
||
// FlaggableLexler wraps a lexer, automatically translating tokens based on flags, if any. | ||
type FlaggableLexler struct { | ||
lex *Lexer // a reference to the lexer used for tokenization | ||
enabledFlags map[string]transformer // flags that are enabled | ||
seenDefinition bool | ||
afterUseIdentifier bool | ||
} | ||
|
||
// NewFlaggableLexler returns a new FlaggableLexler for the given lexer. | ||
func NewFlaggableLexler(lex *Lexer) *FlaggableLexler { | ||
return &FlaggableLexler{ | ||
lex: lex, | ||
enabledFlags: map[string]transformer{}, | ||
} | ||
} | ||
|
||
// Close stops the lexer from running. | ||
func (l *FlaggableLexler) Close() { | ||
l.lex.Close() | ||
} | ||
|
||
// NextToken returns the next token found in the lexer. | ||
func (l *FlaggableLexler) NextToken() Lexeme { | ||
nextToken := l.lex.nextToken() | ||
|
||
// Look for `use somefeature` | ||
if nextToken.Kind == TokenTypeIdentifier { | ||
// Only allowed until we've seen a definition of some kind. | ||
if !l.seenDefinition { | ||
if l.afterUseIdentifier { | ||
if transformer, ok := Flags[nextToken.Value]; ok { | ||
l.enabledFlags[nextToken.Value] = transformer | ||
} | ||
|
||
l.afterUseIdentifier = false | ||
} else { | ||
l.afterUseIdentifier = nextToken.Value == "use" | ||
} | ||
} | ||
} | ||
|
||
if nextToken.Kind == TokenTypeKeyword && nextToken.Value == "definition" { | ||
l.seenDefinition = true | ||
} | ||
if nextToken.Kind == TokenTypeKeyword && nextToken.Value == "caveat" { | ||
l.seenDefinition = true | ||
} | ||
|
||
for _, handler := range l.enabledFlags { | ||
updated, ok := handler(nextToken) | ||
if ok { | ||
return updated | ||
} | ||
} | ||
|
||
return nextToken | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package lexer | ||
|
||
import ( | ||
"slices" | ||
"testing" | ||
|
||
"github.com/authzed/spicedb/pkg/composableschemadsl/input" | ||
) | ||
|
||
var flaggableLexerTests = []lexerTest{ | ||
{"use expiration", "use expiration", []Lexeme{ | ||
{TokenTypeKeyword, 0, "use", ""}, | ||
{TokenTypeWhitespace, 0, " ", ""}, | ||
{TokenTypeKeyword, 0, "expiration", ""}, | ||
tEOF, | ||
}}, | ||
{"use expiration and", "use expiration and", []Lexeme{ | ||
{TokenTypeKeyword, 0, "use", ""}, | ||
{TokenTypeWhitespace, 0, " ", ""}, | ||
{TokenTypeKeyword, 0, "expiration", ""}, | ||
{TokenTypeWhitespace, 0, " ", ""}, | ||
{TokenTypeKeyword, 0, "and", ""}, | ||
tEOF, | ||
}}, | ||
{"expiration as non-keyword", "foo expiration", []Lexeme{ | ||
{TokenTypeIdentifier, 0, "foo", ""}, | ||
{TokenTypeWhitespace, 0, " ", ""}, | ||
{TokenTypeKeyword, 0, "expiration", ""}, | ||
tEOF, | ||
}}, | ||
{"and as non-keyword", "foo and", []Lexeme{ | ||
{TokenTypeIdentifier, 0, "foo", ""}, | ||
{TokenTypeWhitespace, 0, " ", ""}, | ||
{TokenTypeKeyword, 0, "and", ""}, | ||
tEOF, | ||
}}, | ||
{"invalid use flag", "use foobar", []Lexeme{ | ||
{TokenTypeKeyword, 0, "use", ""}, | ||
{TokenTypeWhitespace, 0, " ", ""}, | ||
{TokenTypeIdentifier, 0, "foobar", ""}, | ||
tEOF, | ||
}}, | ||
{"use flag after definition", "definition use expiration", []Lexeme{ | ||
{TokenTypeKeyword, 0, "definition", ""}, | ||
{TokenTypeWhitespace, 0, " ", ""}, | ||
{TokenTypeKeyword, 0, "use", ""}, | ||
{TokenTypeWhitespace, 0, " ", ""}, | ||
{TokenTypeKeyword, 0, "expiration", ""}, | ||
tEOF, | ||
}}, | ||
} | ||
|
||
func TestFlaggableLexer(t *testing.T) { | ||
for _, test := range append(slices.Clone(lexerTests), flaggableLexerTests...) { | ||
t.Run(test.name, func(t *testing.T) { | ||
tokens := performFlaggedLex(&test) | ||
if !equal(tokens, test.tokens) { | ||
t.Errorf("%s: got\n\t%+v\nexpected\n\t%v", test.name, tokens, test.tokens) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func performFlaggedLex(t *lexerTest) (tokens []Lexeme) { | ||
lexer := NewFlaggableLexler(Lex(input.Source(t.name), t.input)) | ||
for { | ||
token := lexer.NextToken() | ||
tokens = append(tokens, token) | ||
if token.Kind == TokenTypeEOF || token.Kind == TokenTypeError { | ||
break | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package lexer | ||
|
||
// FlagExpiration indicates that `expiration` is supported as a first-class | ||
// feature in the schema. | ||
const FlagExpiration = "expiration" | ||
|
||
type transformer func(lexeme Lexeme) (Lexeme, bool) | ||
|
||
// Flags is a map of flag names to their corresponding transformers. | ||
var Flags = map[string]transformer{ | ||
FlagExpiration: func(lexeme Lexeme) (Lexeme, bool) { | ||
// `expiration` becomes a keyword. | ||
if lexeme.Kind == TokenTypeIdentifier && lexeme.Value == "expiration" { | ||
lexeme.Kind = TokenTypeKeyword | ||
return lexeme, true | ||
} | ||
|
||
// `and` becomes a keyword. | ||
if lexeme.Kind == TokenTypeIdentifier && lexeme.Value == "and" { | ||
lexeme.Kind = TokenTypeKeyword | ||
return lexeme, true | ||
} | ||
|
||
return lexeme, false | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,18 @@ var keywords = map[string]struct{}{ | |
"all": {}, | ||
"any": {}, | ||
"partial": {}, | ||
"use": {}, | ||
"expiration": {}, | ||
// Parking lot for future keywords | ||
"and": {}, | ||
"or": {}, | ||
"not": {}, | ||
"under": {}, | ||
"static": {}, | ||
"if": {}, | ||
"where": {}, | ||
"private": {}, | ||
"public": {}, | ||
Comment on lines
+87
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is one change that wasn't a direct copy. |
||
} | ||
|
||
// IsKeyword returns whether the specified input string is a reserved keyword. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth reworking these tests?
use
andexpiration
are now keywords, so the semantics of the tests don't completely scan.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think so; we should add them in as formal keywords in the lexer tests