start working on grammar

This commit is contained in:
Daniel Bulant 2024-02-13 22:20:51 +01:00
parent bfa5f9ef39
commit 9f75294bfa
6 changed files with 253 additions and 16 deletions

8
.gitignore vendored
View file

@ -1,2 +1,8 @@
/node_modules
/pnpm-lock.yaml
/pnpm-lock.yaml
src/surrealql.grammar.*
/dist
/test/*.js
/test/*.d.ts
/test/*.d.ts.map
.tern-*

6
.npmignore Normal file
View file

@ -0,0 +1,6 @@
/src
/test
/node_modules
.tern-*
rollup.config.js
tsconfig.json

View file

@ -1,18 +1,30 @@
{
"name": "lang-surrealql",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"prepare": "lezer-generator src/surrealql.grammar -o src/surrealql.grammar.js"
"description": "SurrealQL language support for CodeMirror 6",
"main": "dist/index.cjs",
"type": "module",
"exports": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"keywords": [],
"author": "",
"license": "ISC",
"scripts": {
"test": "cm-runtests",
"prepare": "cm-buildhelper src/surrealql.ts"
},
"types": "dist/index.d.ts",
"module": "dist/index.js",
"sideEffects": false,
"license": "MIT",
"devDependencies": {
"@lezer/generator": "^1.6.0"
"@codemirror/buildhelper": "^1.0.0"
},
"dependencies": {
"@codemirror/autocomplete": "^6.0.0",
"@lezer/common": "^1.2.0",
"@lezer/highlight": "^1.0.0",
"@codemirror/language": "^6.0.0",
"@codemirror/state": "^6.0.0",
"@lezer/lr": "^1.4.0"
}
}

View file

@ -1,10 +1,162 @@
@top File { (Identifier | Number | String)+ }
@top Script {
(statement SEMI)*
statement
}
@skip { space }
@top SingleStatement {
statement
}
@skip { whitespace | LineComment }
kw<term> { @specialize[@name={term}]<identifier, term> }
ckw<term> { @extend[@name={term}]<identifier, term> }
Table { identifier }
RecordID {
Table ":"
(identifier | RIDStart RIDContent RIDEnd | RIDDelim RIDDelimContent RIDDelim | Array | Object)
}
RecordRange {
Table ":"
(
number? (".." | "..=") number? |
Array? (".." | "..=") Array? |
Object? (".." | "..=") Object?
)
}
GeneratedRecordID {
Table ":" FunctionCall
}
Variable {
kw<"$"> identifier
}
StringPrefix {
ckw<"s"> | ckw<"r"> | ckw<"d"> | ckw<"u">
}
@skip {} {
String[isolate] {
StringPrefix?
('"' (stringContentDouble | Escape)* ('"' | "\n") |
"'" (stringContentSingle | Escape)* ("'" | "\n"))
}
}
@precedence { decimal @left, float @left, integer @left }
Integer { int }
Decimal { int ("." int)? "dec" }
Float { int (("." int) "f"? | "f" ) }
number { Decimal !decimal | Float !float | Integer !integer }
Array {
"[" (expression (COMMA expression)*)? "]"
}
Object {
"{" (identifier ":" expression (COMMA identifier ":" expression)*)? "}"
}
Namespace {
identifier
}
FunctionName {
identifier
}
FunctionCall {
(Namespace "::" )? FunctionName "(" (expression (COMMA expression)*)? ")"
}
Constant {
(Namespace "::")?
identifier
}
Cast {
"<" identifier ">" expression
}
Future {
"<future>" "{"
expression
"}"
}
Column {
identifier
}
ReturnStatement {
kw<"return"> expression
}
BeginStatement {
kw<"begin"> kw<"transaction">?
}
BreakStatement {
kw<"break">
}
CancelStatement {
kw<"cancel"> kw<"transaction">?
}
expression[@isGroup=Expression] {
String |
number |
Array |
Object |
FunctionCall |
Future |
Variable |
Cast |
RecordID |
Column
}
statement[@isGroup=Statement] {
ReturnStatement |
BeginStatement |
BreakStatement |
CancelStatement
}
@local tokens {
blockCommentEnd { "*/" }
blockCommentNewline { "\n" }
@else blockCommentContent
}
@skip {} { BlockComment { "/*" (blockCommentContent | blockCommentNewline)* blockCommentEnd } }
@tokens {
space { @whitespace+ }
Identifier { $[A-Za-z_]+ }
Number { $[0-9]+ }
String { '"' !["]* '"' }
}
whitespace { @whitespace+ }
LineComment { ("//" | "-- ") ![\n]* }
int { @digit+ }
identifier { $[a-zA-Z] $[a-zA-Z0-9_]* }
RIDDelim { "`" }
RIDStart { "⟨" }
RIDEnd { "⟩" }
RIDDelimContent { $[^`]+ }
RIDContent { $[^⟨⟩]+ }
Divide { "/" }
Multiply { "*" }
Add { "+" }
Subtract { "-" }
LessThan { "<" }
GreaterThan { ">" }
Escape {
"\\" ("x" hex hex | "u" ("{" hex+ "}" | hex hex hex hex) | ![xu])
}
hex { @digit | $[a-fA-F] }
stringContentSingle { ![\\\n']+ }
stringContentDouble { ![\\\n"]+ }
EQ { "=" }
COMMA { "," }
SEMI { ";" }
"(" ")" "[" "]" "{" "}"
"."
}
@detectDelim

59
src/surrealql.ts Normal file
View file

@ -0,0 +1,59 @@
import { LanguageSupport } from "@codemirror/language"
import {parser as baseParser} from "./surrealql.grammar"
import { LRLanguage } from "@codemirror/language"
import { foldNodeProp } from "@codemirror/language";
import { indentNodeProp } from "@codemirror/language";
import { continuedIndent } from "@codemirror/language";
import { styleTags, tags as t } from "@lezer/highlight";
export { baseParser }
export let parser = baseParser.configure({
props: [
foldNodeProp.add({
BlockComment(tree) { return { from: tree.from + 2, to: tree.to - 2 }}
}),
indentNodeProp.add({
Statement: continuedIndent()
}),
styleTags({
Keyword: t.keyword,
Type: t.typeName,
Builtin: t.standard(t.name),
Bits: t.number,
Bytes: t.string,
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),
LineComment: t.lineComment,
BlockComment: t.blockComment,
Operator: t.operator,
"Semi Punctuation": t.punctuation,
"( )": t.paren,
"{ }": t.brace,
"[ ]": t.squareBracket
})
]
});
const language = LRLanguage.define({
name: "surrealql",
parser: parser,
languageData: {
commentTokens: {line: "//", block: {open: "/*", close: "*/"}},
closeBrackets: {
brackets: ["(", "[", "{", "'", '"', "`"]
}
}
});
export function surrealql() {
return new LanguageSupport(
language,
[]
);
};

View file

@ -0,0 +1,2 @@
import {baseParser} from "./dist/index.js"
console.log(baseParser.parse('begin transaction').toString())