Skip to content

Commit

Permalink
Add lexer/detector for zed
Browse files Browse the repository at this point in the history
Reference: https://docs.authzed.com/reference/schema-lang

Used by project SpiceDB (https://github.com/authzed/spicedb), a database system for managing security-critical permissions checking, inspired by Google's Zanzibar paper
  • Loading branch information
josephschorr authored and alecthomas committed Oct 27, 2021
1 parent 2023798 commit 0fcd2d8
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lexers/testdata/zed.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Project is a project.
*/
definition test/project {
relation issue_creator: role#member
relation issue_assigner: role#member
relation any_issue_resolver: role#member
relation assigned_issue_resolver: role#member
relation comment_creator: role#member
relation comment_deleter: role#member
relation role_manager: role#member

permission create_issue = issue_creator
permission create_role = role_manager
}

definition role {
/** project is the project */
relation project: project
relation member: user
relation built_in_role: project

permission delete = project->role_manager - built_in_role->role_manager
permission add_user = project->role_manager
permission add_permission = project->role_manager & built_in_role->role_manager

// with arrows!
permission remove_permission = project->role_manager + built_in_role->role_manager
}
175 changes: 175 additions & 0 deletions lexers/testdata/zed.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
[
{"type":"CommentMultiline","value":"/**\n * Project is a project.\n */"},
{"type":"TextWhitespace","value":"\n"},
{"type":"KeywordType","value":"definition"},
{"type":"TextWhitespace","value":" "},
{"type":"NameNamespace","value":"test/"},
{"type":"Name","value":"project"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"KeywordNamespace","value":"relation"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"issue_creator"},
{"type":"Operator","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"role"},
{"type":"NameVariable","value":"#member"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"KeywordNamespace","value":"relation"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"issue_assigner"},
{"type":"Operator","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"role"},
{"type":"NameVariable","value":"#member"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"KeywordNamespace","value":"relation"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"any_issue_resolver"},
{"type":"Operator","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"role"},
{"type":"NameVariable","value":"#member"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"KeywordNamespace","value":"relation"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"assigned_issue_resolver"},
{"type":"Operator","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"role"},
{"type":"NameVariable","value":"#member"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"KeywordNamespace","value":"relation"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"comment_creator"},
{"type":"Operator","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"role"},
{"type":"NameVariable","value":"#member"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"KeywordNamespace","value":"relation"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"comment_deleter"},
{"type":"Operator","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"role"},
{"type":"NameVariable","value":"#member"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"KeywordNamespace","value":"relation"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"role_manager"},
{"type":"Operator","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"role"},
{"type":"NameVariable","value":"#member"},
{"type":"TextWhitespace","value":"\n\t\n\t"},
{"type":"KeywordDeclaration","value":"permission"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"create_issue"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"issue_creator"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"KeywordDeclaration","value":"permission"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"create_role"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"role_manager"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"KeywordType","value":"definition"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"role"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"CommentMultiline","value":"/** project is the project */"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"KeywordNamespace","value":"relation"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"project"},
{"type":"Operator","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"project"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"KeywordNamespace","value":"relation"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"member"},
{"type":"Operator","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"user"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"KeywordNamespace","value":"relation"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"built_in_role"},
{"type":"Operator","value":":"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"project"},
{"type":"TextWhitespace","value":"\n\t\n\t"},
{"type":"KeywordDeclaration","value":"permission"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"delete"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"project"},
{"type":"Operator","value":"-\u003e"},
{"type":"Name","value":"role_manager"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"-"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"built_in_role"},
{"type":"Operator","value":"-\u003e"},
{"type":"Name","value":"role_manager"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"KeywordDeclaration","value":"permission"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"add_user"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"project"},
{"type":"Operator","value":"-\u003e"},
{"type":"Name","value":"role_manager"},
{"type":"TextWhitespace","value":"\n\t"},
{"type":"KeywordDeclaration","value":"permission"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"add_permission"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"project"},
{"type":"Operator","value":"-\u003e"},
{"type":"Name","value":"role_manager"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"\u0026"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"built_in_role"},
{"type":"Operator","value":"-\u003e"},
{"type":"Name","value":"role_manager"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"CommentSingle","value":"// with arrows!\n"},
{"type":"TextWhitespace","value":"\t"},
{"type":"KeywordDeclaration","value":"permission"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"remove_permission"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"project"},
{"type":"Operator","value":"-\u003e"},
{"type":"Name","value":"role_manager"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"+"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"built_in_role"},
{"type":"Operator","value":"-\u003e"},
{"type":"Name","value":"role_manager"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":"}"}
]
53 changes: 53 additions & 0 deletions lexers/z/zed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package z

import (
"strings"

. "github.com/alecthomas/chroma" // nolint
"github.com/alecthomas/chroma/lexers/internal"
)

// Zed lexer.
var Zed = internal.Register(MustNewLazyLexer(
&Config{
Name: "Zed",
Aliases: []string{"zed"},
Filenames: []string{"*.zed"},
MimeTypes: []string{"text/zed"},
},
zedRules,
).SetAnalyser(func(text string) float32 {
if strings.Contains(text, "definition ") && strings.Contains(text, "relation ") && strings.Contains(text, "permission ") {
return 0.9
}
if strings.Contains(text, "definition ") {
return 0.5
}
if strings.Contains(text, "relation ") {
return 0.5
}
if strings.Contains(text, "permission ") {
return 0.25
}
return 0.0
}))

func zedRules() Rules {
return Rules{
"root": {
{`\n`, TextWhitespace, nil},
{`\s+`, TextWhitespace, nil},
{`//.*?\n`, CommentSingle, nil},
{`/(\\\n)?[*][\w\W]*?[*](\\\n)?/`, CommentMultiline, nil},
{`/(\\\n)?[*][\w\W]*`, CommentMultiline, nil},
{Words(``, `\b`, `definition`), KeywordType, nil},
{Words(``, `\b`, `relation`), KeywordNamespace, nil},
{Words(``, `\b`, `permission`), KeywordDeclaration, nil},
{`[a-zA-Z_]\w*/`, NameNamespace, nil},
{`[a-zA-Z_]\w*`, Name, nil},
{`#[a-zA-Z_]\w*`, NameVariable, nil},
{`[+%=><|^!?/\-*&~:]`, Operator, nil},
{`[{}()\[\],.;]`, Punctuation, nil},
},
}
}

0 comments on commit 0fcd2d8

Please sign in to comment.