mirror of
https://github.com/danbulant/lang-surrealql
synced 2026-07-07 12:01:15 +00:00
start working on grammar
This commit is contained in:
parent
bfa5f9ef39
commit
9f75294bfa
6 changed files with 253 additions and 16 deletions
8
.gitignore
vendored
8
.gitignore
vendored
|
|
@ -1,2 +1,8 @@
|
||||||
/node_modules
|
/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
6
.npmignore
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
/src
|
||||||
|
/test
|
||||||
|
/node_modules
|
||||||
|
.tern-*
|
||||||
|
rollup.config.js
|
||||||
|
tsconfig.json
|
||||||
28
package.json
28
package.json
|
|
@ -1,18 +1,30 @@
|
||||||
{
|
{
|
||||||
"name": "lang-surrealql",
|
"name": "lang-surrealql",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "SurrealQL language support for CodeMirror 6",
|
||||||
"main": "index.js",
|
"main": "dist/index.cjs",
|
||||||
"scripts": {
|
"type": "module",
|
||||||
"prepare": "lezer-generator src/surrealql.grammar -o src/surrealql.grammar.js"
|
"exports": {
|
||||||
|
"import": "./dist/index.js",
|
||||||
|
"require": "./dist/index.cjs"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"scripts": {
|
||||||
"author": "",
|
"test": "cm-runtests",
|
||||||
"license": "ISC",
|
"prepare": "cm-buildhelper src/surrealql.ts"
|
||||||
|
},
|
||||||
|
"types": "dist/index.d.ts",
|
||||||
|
"module": "dist/index.js",
|
||||||
|
"sideEffects": false,
|
||||||
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@lezer/generator": "^1.6.0"
|
"@codemirror/buildhelper": "^1.0.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"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"
|
"@lezer/lr": "^1.4.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
@tokens {
|
||||||
space { @whitespace+ }
|
whitespace { @whitespace+ }
|
||||||
Identifier { $[A-Za-z_]+ }
|
LineComment { ("//" | "-- ") ![\n]* }
|
||||||
Number { $[0-9]+ }
|
|
||||||
String { '"' !["]* '"' }
|
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
59
src/surrealql.ts
Normal 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,
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
};
|
||||||
2
test.js
2
test.js
|
|
@ -0,0 +1,2 @@
|
||||||
|
import {baseParser} from "./dist/index.js"
|
||||||
|
console.log(baseParser.parse('begin transaction').toString())
|
||||||
Loading…
Reference in a new issue