-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2141 from josephschorr/rel-expiration-parser
Parser changes for supporting relationship expiration
- Loading branch information
Showing
24 changed files
with
546 additions
and
78 deletions.
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
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/schemadsl/input" | ||
) | ||
|
||
var flaggableLexerTests = []lexerTest{ | ||
{"use expiration", "use expiration", []Lexeme{ | ||
{TokenTypeIdentifier, 0, "use", ""}, | ||
{TokenTypeWhitespace, 0, " ", ""}, | ||
{TokenTypeKeyword, 0, "expiration", ""}, | ||
tEOF, | ||
}}, | ||
{"use expiration and", "use expiration and", []Lexeme{ | ||
{TokenTypeIdentifier, 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, " ", ""}, | ||
{TokenTypeIdentifier, 0, "expiration", ""}, | ||
tEOF, | ||
}}, | ||
{"and as non-keyword", "foo and", []Lexeme{ | ||
{TokenTypeIdentifier, 0, "foo", ""}, | ||
{TokenTypeWhitespace, 0, " ", ""}, | ||
{TokenTypeIdentifier, 0, "and", ""}, | ||
tEOF, | ||
}}, | ||
{"invalid use flag", "use foobar", []Lexeme{ | ||
{TokenTypeIdentifier, 0, "use", ""}, | ||
{TokenTypeWhitespace, 0, " ", ""}, | ||
{TokenTypeIdentifier, 0, "foobar", ""}, | ||
tEOF, | ||
}}, | ||
{"use flag after definition", "definition use expiration", []Lexeme{ | ||
{TokenTypeKeyword, 0, "definition", ""}, | ||
{TokenTypeWhitespace, 0, " ", ""}, | ||
{TokenTypeIdentifier, 0, "use", ""}, | ||
{TokenTypeWhitespace, 0, " ", ""}, | ||
{TokenTypeIdentifier, 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.