From b7aece5302aa4d2e4b1fb83fc792b54a59c4c178 Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Sat, 17 Feb 2024 15:34:38 +0100 Subject: [PATCH] improved grammar and code highlight --- src/surrealql.grammar | 75 ++++++++++++++++++++++++++++++------------- src/surrealql.ts | 37 +++++++++++++-------- 2 files changed, 76 insertions(+), 36 deletions(-) diff --git a/src/surrealql.grammar b/src/surrealql.grammar index 7dc351b..79be135 100644 --- a/src/surrealql.grammar +++ b/src/surrealql.grammar @@ -1,5 +1,5 @@ @top Script { - (statement SEMI)* + (statement Semi)* statement } @@ -59,15 +59,21 @@ StringPrefix { Integer { int } Decimal { int !int ("." int)? "dec" } Float { int !int (("." int) "f"? | "f" ) } -Duration { int ("d" | "h" | "m" | "s" | "ms" | "us" | "ns") } +Duration { int DurationUnit } +DurationUnit { + "d" | "h" | "m" | "s" | "ms" | "us" | "ns" +} number { Decimal | Float | Integer } Array { - "[" (expression (COMMA expression)*)? "]" + "[" (expression (Comma expression)*)? "]" +} +Property { + identifier } Object { - "{" (identifier ":" expression (COMMA identifier ":" expression)*)? "}" + "{" (Property ":" expression (Comma Property ":" expression)*)? "}" } Namespace { identifier @@ -76,7 +82,7 @@ FunctionName { identifier } FunctionCall { - (Namespace "::" )? FunctionName "(" (expression (COMMA expression)*)? ")" + (Namespace "::" )* FunctionName "(" (expression (Comma expression)*)? ")" } Constant { (Namespace "::")? @@ -104,9 +110,9 @@ BinaryExpression { Comparison | "@" identifier "@" ) expression | expression !exp Raise expression | - expression !times (Divide | Multiply) expression | - expression !plus (Add | Subtract) expression | - expression !dot (".*"+ | ("." "*."*) expression) | + expression !times (DivideOrMultiply) expression | + expression !plus (AddOrSubtract) expression | + expression !dot (("." "*")+ | ("." "*")* "." expression) | expression? !dot "->" expression | expression !array ("[" expression "]") } @@ -114,17 +120,20 @@ BinaryExpression { ReturnStatement { kw<"return"> expression } +maybeTransaction { + kw<"transaction">? +} BeginStatement { - kw<"begin"> kw<"transaction">? + kw<"begin"> maybeTransaction } BreakStatement { kw<"break"> } CancelStatement { - kw<"cancel"> kw<"transaction">? + kw<"cancel"> maybeTransaction } CommitStatement { - kw<"commit"> kw<"transaction">? + kw<"commit"> maybeTransaction } ContinueStatement { kw<"continue"> @@ -172,16 +181,16 @@ maybeBy { kw<"by">? } selectWith { - ("with" (kw<"noindex"> | kw<"index"> identifier (COMMA identifier)))? + ("with" (kw<"noindex"> | kw<"index"> identifier (Comma identifier)))? } maybeOnly { kw<"only">? } selectOrder { - ("order" maybeBy orderBy (COMMA orderBy)*)? + ("order" maybeBy orderBy (Comma orderBy)*)? } selectFrom { - ("from" maybeOnly expression (COMMA expression)*)? + ("from" maybeOnly expression (Comma expression)*)? } Where { "where" expression @@ -189,13 +198,13 @@ Where { SelectStatement { kw<"select"> maybeValue - (Field (COMMA Field)*) - ("omit" Field (COMMA Field)*)? + (Field (Comma Field)*) + ("omit" Field (Comma Field)*)? selectFrom selectWith Where? ("split" maybeAt expression)? - ("group" maybeBy expression (COMMA expression)*)? + ("group" maybeBy expression (Comma expression)*)? selectOrder ("limit" maybeBy expression)? ("start" maybeAt expression)? @@ -204,6 +213,20 @@ SelectStatement { ("explain" kw<"full">?)? } +ForStatement { + kw<"for"> expression "in" expression "{" (statement Semi)* statement "}" +} + +// IfStatement { +// kw<"if"> expression "then"? expression +// ("else" "if" expression "then"? expression)* +// ("else" expression)? +// } + +Bool { kw<"true"> | kw<"false"> } +None { kw<"none"> } +Null { kw<"null"> } + expression { String | number | @@ -217,7 +240,10 @@ expression { RecordID | Column | ParenthesizedExpression | - BinaryExpression + BinaryExpression | + Bool | + None | + Null } ParenthesizedExpression { "(" (expression | statement) ")" @@ -236,7 +262,9 @@ statement[@isGroup=Statement] { ShowStatement | LetStatement | InfoStatement | - SelectStatement + SelectStatement | + ForStatement // | + // IfStatement } @local tokens { @@ -254,7 +282,6 @@ statement[@isGroup=Statement] { int { @digit+ } identifier { $[a-zA-Z] $[a-zA-Z0-9_]* } - columnIdentifier { identifier | "*" } RIDDelim { "`" } RIDStart { "⟨" } @@ -264,8 +291,10 @@ statement[@isGroup=Statement] { Divide { "/" | "÷" } Multiply { "*" | "×" } + DivideOrMultiply { Divide | Multiply } Add { "+" } Subtract { "-" } + AddOrSubtract { Add | Subtract } Raise { "**" } Escape { @@ -275,8 +304,8 @@ statement[@isGroup=Statement] { stringContentSingle { ![\\']+ } stringContentDouble { ![\\"]+ } - COMMA { "," } - SEMI { ";" } + Comma { "," } + Semi { ";" } Comparison { "??" | "?:" | "=" | "IS" | @@ -310,7 +339,9 @@ statement[@isGroup=Statement] { "->" @precedence { "/*", LineComment, Divide } + @precedence { "/*", LineComment, DivideOrMultiply} @precedence { LineComment, Subtract } + @precedence { LineComment, AddOrSubtract } } @detectDelim \ No newline at end of file diff --git a/src/surrealql.ts b/src/surrealql.ts index 3a4c896..c47ee85 100644 --- a/src/surrealql.ts +++ b/src/surrealql.ts @@ -17,22 +17,31 @@ export let parser = baseParser.configure({ Statement: continuedIndent() }), styleTags({ - Keyword: t.keyword, - Type: t.typeName, - Builtin: t.standard(t.name), - Bits: t.number, - Bytes: t.string, + "StringPrefix as asc desc": t.keyword, + Escape: t.escape, Bool: t.bool, - Null: t.null, - Number: t.number, - String: t.string, - Identifier: t.name, - QuotedIdentifier: t.special(t.string), - SpecialVar: t.special(t.name), + "DivideOrMultiply AddOrSubtract": t.arithmeticOperator, + "let ": t.definitionKeyword, + Namespace: t.namespace, + FunctionName: t.function(t.variableName), + Variable: t.variableName, + "*": t.atom, + Integer: t.integer, + "Decimal Float": t.float, + Duration: t.number, LineComment: t.lineComment, BlockComment: t.blockComment, - Operator: t.operator, - "Semi Punctuation": t.punctuation, + String: t.string, + "return break ForStatement/for if else then": t.controlKeyword, + "DurationUnit": t.unit, + "None Null": t.null, + "RecordID": t.special(t.variableName), + "Comparison @": t.compareOperator, + "And Or": t.logicOperator, + "BinaryExpression/. ->": t.derefOperator, + ": :: ..=": t.punctuation, + "Column Property": t.propertyName, + "Semi Comma": t.separator, "( )": t.paren, "{ }": t.brace, "[ ]": t.squareBracket @@ -40,7 +49,7 @@ export let parser = baseParser.configure({ ] }); -const language = LRLanguage.define({ +export const language = LRLanguage.define({ name: "surrealql", parser: parser, languageData: {