From 299c4d93597b3065e6a1017ebe692cde66fc5e39 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Mon, 11 Oct 2021 01:03:21 +0200 Subject: feat(web): experiment with Lezer parser --- .../src/main/js/editor/problemLanguageSupport.ts | 82 ++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 language-web/src/main/js/editor/problemLanguageSupport.ts (limited to 'language-web/src/main/js/editor/problemLanguageSupport.ts') diff --git a/language-web/src/main/js/editor/problemLanguageSupport.ts b/language-web/src/main/js/editor/problemLanguageSupport.ts new file mode 100644 index 00000000..2bf7c7a4 --- /dev/null +++ b/language-web/src/main/js/editor/problemLanguageSupport.ts @@ -0,0 +1,82 @@ +import { styleTags, tags as t } from '@codemirror/highlight'; +import { + foldInside, + foldNodeProp, + indentNodeProp, + LanguageSupport, + LRLanguage, +} from '@codemirror/language'; +import { LRParser } from '@lezer/lr'; + +import { parser } from '../../../../build/generated/sources/lezer/problem'; +import { + foldBlockComment, + foldConjunction, + foldDeclaration, +} from './folding'; +import { + indentBlockComment, + indentDeclaration, + indentPredicate, +} from './indentation'; + +const parserWithMetadata = (parser as LRParser).configure({ + props: [ + styleTags({ + LineComment: t.lineComment, + BlockComment: t.blockComment, + 'problem class enum pred unique scope': t.definitionKeyword, + 'abstract refers contains opposite error default': t.modifier, + 'true false unknown error': t.keyword, + NotOp: t.keyword, + UnknownOp: t.keyword, + OrOp: t.keyword, + StarArgument: t.keyword, + 'IntMult StarMult Real': t.number, + StarMult: t.number, + 'RelationName/QualifiedName': t.typeName, + 'UniqueNodeName/QualifiedName': t.atom, + 'VariableName/QualifiedName': t.variableName, + '{ }': t.brace, + '( )': t.paren, + '[ ]': t.squareBracket, + '. .. , :': t.separator, + '<->': t.definitionOperator, + }), + indentNodeProp.add({ + ProblemDeclaration: indentDeclaration, + UniqueDeclaration: indentDeclaration, + ScopeDeclaration: indentDeclaration, + PredicateBody: indentPredicate, + BlockComment: indentBlockComment, + }), + foldNodeProp.add({ + ClassBody: foldInside, + EnumBody: foldInside, + ParameterList: foldInside, + PredicateBody: foldInside, + Conjunction: foldConjunction, + UniqueDeclaration: foldDeclaration, + ScopeDeclaration: foldDeclaration, + BlockComment: foldBlockComment, + }), + ], +}); + +const problemLanguage = LRLanguage.define({ + parser: parserWithMetadata, + languageData: { + commentTokens: { + block: { + open: '/*', + close: '*/', + }, + line: '%', + }, + indentOnInput: /^\s*(?:\{|\}|\(|\)|;|\.)$/, + }, +}); + +export function problemLanguageSupport(): LanguageSupport { + return new LanguageSupport(problemLanguage); +} -- cgit v1.2.3-54-g00ecf