-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add syntax highlighting through Lezer
- Loading branch information
Showing
7 changed files
with
189 additions
and
8 deletions.
There are no files selected for viewing
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
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
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,53 @@ | ||
import { parser } from "./percival.grammar"; | ||
import { | ||
foldNodeProp, | ||
foldInside, | ||
indentNodeProp, | ||
LanguageSupport, | ||
LRLanguage, | ||
} from "@codemirror/language"; | ||
import { styleTags, tags as t } from "@codemirror/highlight"; | ||
|
||
let parserWithMetadata = parser.configure({ | ||
props: [ | ||
styleTags({ | ||
Identifier: t.local(t.variableName), | ||
TableName: t.definition(t.variableName), | ||
PropName: t.definition(t.propertyName), | ||
String: t.string, | ||
Number: t.number, | ||
Expr: [t.regexp, t.emphasis], | ||
LineComment: t.lineComment, | ||
BlockComment: t.blockComment, | ||
ImportKeyword: t.keyword, | ||
FromKeyword: t.keyword, | ||
Goal: t.string, | ||
"( )": t.paren, | ||
":-": t.punctuation, | ||
".": t.punctuation, | ||
":": t.punctuation, | ||
",": t.punctuation, | ||
}), | ||
indentNodeProp.add({ | ||
Rule: (context) => context.column(context.node.from) + context.unit, | ||
}), | ||
foldNodeProp.add({ | ||
Rule: foldInside, | ||
}), | ||
], | ||
}); | ||
|
||
export const percivalLanguage = LRLanguage.define({ | ||
parser: parserWithMetadata, | ||
languageData: { | ||
commentTokens: { | ||
line: "//", | ||
block: { open: "/*", close: "*/" }, | ||
}, | ||
}, | ||
}); | ||
|
||
/** CodeMirror extension for Percival language support. */ | ||
export function percival() { | ||
return new LanguageSupport(percivalLanguage, []); | ||
} |
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,73 @@ | ||
// Lezer LR(1) grammar for Percival, used for syntax highlighting in CodeMirror. | ||
|
||
@top Program { entry* } | ||
|
||
entry { | ||
Rule | | ||
Import | ||
} | ||
|
||
Rule { | ||
Fact "." | | ||
Fact ":-" (Clause ",")* Clause "." | ||
} | ||
|
||
Clause { | ||
Fact | | ||
Expr | ||
} | ||
|
||
Fact { | ||
TableName "(" ((Prop ",")* Prop)? ")" | ||
} | ||
|
||
Prop { | ||
PropName (":" Value)? | ||
} | ||
|
||
Value { | ||
Identifier | | ||
literal | | ||
Expr | ||
} | ||
|
||
literal { | ||
Number | | ||
String | ||
} | ||
|
||
Import { | ||
ImportKeyword TableName FromKeyword String | ||
} | ||
|
||
@tokens { | ||
Identifier { $[a-zA-Z_] $[a-zA-Z_0-9]* } | ||
|
||
TableName { $[a-zA-Z_] $[a-zA-Z_0-9]* } | ||
|
||
PropName { $[a-zA-Z_] $[a-zA-Z_0-9]* } | ||
|
||
Number { $[-+]? ($[0-9] "_"?)+ ("." ($[0-9] "_"?)+)? ($[eE] $[-+]? ($[0-9] "_"?)+)? } | ||
|
||
String { '"' (!["\\] | "\\" _)* '"' } | ||
|
||
Expr { "`" ![`]* "`" } | ||
|
||
LineComment { "//" ![\n]* } | ||
|
||
BlockComment { "/*" _*? "*/" } | ||
|
||
space { $[ \t\n\r]+ } | ||
|
||
"(" ")" | ||
|
||
":" ":-" "." "," | ||
|
||
ImportKeyword { "@import" } | ||
|
||
FromKeyword { "from" } | ||
} | ||
|
||
@skip { space | LineComment | BlockComment } | ||
|
||
@detectDelim |
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,3 @@ | ||
import { Parser } from "lezer"; | ||
|
||
export declare const parser: Parser; |
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