diff options
author | Kristóf Marussy <marussy@mit.bme.hu> | 2024-02-19 00:44:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-19 00:44:05 +0100 |
commit | 201c3da912aeaab8a3f51b84198b35c7936d0446 (patch) | |
tree | e427986134c41b540b10b9a9576f5a1788d8fbed /subprojects/frontend/src | |
parent | chore(deps): bump dependencies (diff) | |
parent | docs: update README (diff) | |
download | refinery-201c3da912aeaab8a3f51b84198b35c7936d0446.tar.gz refinery-201c3da912aeaab8a3f51b84198b35c7936d0446.tar.zst refinery-201c3da912aeaab8a3f51b84198b35c7936d0446.zip |
Merge pull request #53 from kris7t/imports
Module and import resolution support
Diffstat (limited to 'subprojects/frontend/src')
5 files changed, 84 insertions, 16 deletions
diff --git a/subprojects/frontend/src/editor/EditorTheme.ts b/subprojects/frontend/src/editor/EditorTheme.ts index 1cad4a36..4978c7f7 100644 --- a/subprojects/frontend/src/editor/EditorTheme.ts +++ b/subprojects/frontend/src/editor/EditorTheme.ts | |||
@@ -40,7 +40,9 @@ function createTypeHashStyles( | |||
40 | export default styled('div', { | 40 | export default styled('div', { |
41 | name: 'EditorTheme', | 41 | name: 'EditorTheme', |
42 | shouldForwardProp: (propName) => | 42 | shouldForwardProp: (propName) => |
43 | propName !== 'showLineNumbers' && propName !== 'showActiveLine', | 43 | propName !== 'showLineNumbers' && |
44 | propName !== 'showActiveLine' && | ||
45 | propName !== 'colorIdentifiers', | ||
44 | })<{ | 46 | })<{ |
45 | showLineNumbers: boolean; | 47 | showLineNumbers: boolean; |
46 | showActiveLine: boolean; | 48 | showActiveLine: boolean; |
@@ -126,7 +128,7 @@ export default styled('div', { | |||
126 | color: theme.palette.text.secondary, | 128 | color: theme.palette.text.secondary, |
127 | }, | 129 | }, |
128 | }, | 130 | }, |
129 | '.tok-problem-individual': { | 131 | '.tok-problem-atom': { |
130 | '&, & .tok-variableName': { | 132 | '&, & .tok-variableName': { |
131 | color: theme.palette.text.primary, | 133 | color: theme.palette.text.primary, |
132 | }, | 134 | }, |
diff --git a/subprojects/frontend/src/language/problem.grammar b/subprojects/frontend/src/language/problem.grammar index ce3baa02..b69ee73f 100644 --- a/subprojects/frontend/src/language/problem.grammar +++ b/subprojects/frontend/src/language/problem.grammar | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | 2 | * SPDX-FileCopyrightText: 2021-2024 The Refinery Authors <https://refinery.tools/> |
3 | * | 3 | * |
4 | * SPDX-License-Identifier: EPL-2.0 | 4 | * SPDX-License-Identifier: EPL-2.0 |
5 | */ | 5 | */ |
@@ -22,13 +22,16 @@ | |||
22 | @top Problem { statement* } | 22 | @top Problem { statement* } |
23 | 23 | ||
24 | statement { | 24 | statement { |
25 | ImportStatement { | ||
26 | kw<"import"> ModuleName (kw<"as"> ModuleName)? "." | ||
27 | } | | ||
25 | Assertion { | 28 | Assertion { |
26 | kw<"default">? (NotOp | UnknownOp)? RelationName | 29 | kw<"default">? (NotOp | UnknownOp)? RelationName |
27 | ParameterList<AssertionArgument> | 30 | ParameterList<AssertionArgument> |
28 | (":" Expr)? "." | 31 | (":" Expr)? "." |
29 | } | | 32 | } | |
30 | ProblemDeclaration { | 33 | ProblemDeclaration { |
31 | kw<"problem"> QualifiedName "." | 34 | (ckw<"module"> | kw<"problem">) ModuleName "." |
32 | } | | 35 | } | |
33 | ClassDefinition { | 36 | ClassDefinition { |
34 | kw<"abstract">? kw<"class"> RelationName | 37 | kw<"abstract">? kw<"class"> RelationName |
@@ -37,7 +40,7 @@ statement { | |||
37 | } | | 40 | } | |
38 | EnumDefinition { | 41 | EnumDefinition { |
39 | kw<"enum"> RelationName | 42 | kw<"enum"> RelationName |
40 | (EnumBody { "{" sep<",", IndividualNodeName> "}" } | ".") | 43 | (EnumBody { "{" sep<",", AtomNodeName> "}" } | ".") |
41 | } | | 44 | } | |
42 | PredicateDefinition { | 45 | PredicateDefinition { |
43 | ( | 46 | ( |
@@ -56,8 +59,11 @@ statement { | |||
56 | // RuleName ParameterList<Parameter>? | 59 | // RuleName ParameterList<Parameter>? |
57 | // RuleBody { ":" sep<OrOp, Conjunction> "==>" sep<OrOp, Consequent> "." } | 60 | // RuleBody { ":" sep<OrOp, Conjunction> "==>" sep<OrOp, Consequent> "." } |
58 | //} | | 61 | //} | |
59 | IndividualDeclaration { | 62 | AtomDeclaration { |
60 | kw<"indiv"> sep<",", IndividualNodeName> "." | 63 | kw<"declare">? ckw<"atom"> sep<",", AtomNodeName> "." |
64 | } | | ||
65 | NodeDeclaration { | ||
66 | (kw<"declare"> | kw<"declare">? ckw<"multi">) sep<",", NodeName> "." | ||
61 | } | | 67 | } | |
62 | ScopeDeclaration { | 68 | ScopeDeclaration { |
63 | kw<"scope"> sep<",", ScopeElement> "." | 69 | kw<"scope"> sep<",", ScopeElement> "." |
@@ -161,13 +167,15 @@ RelationName { QualifiedName ~name } | |||
161 | 167 | ||
162 | //RuleName { QualifiedName } | 168 | //RuleName { QualifiedName } |
163 | 169 | ||
164 | IndividualNodeName { QualifiedName } | 170 | AtomNodeName { QualifiedName } |
165 | 171 | ||
166 | VariableName[@dynamicPrecedence=10] { QualifiedName ~name } | 172 | VariableName[@dynamicPrecedence=10] { QualifiedName ~name } |
167 | 173 | ||
168 | NodeName { QualifiedName } | 174 | NodeName { QualifiedName } |
169 | 175 | ||
170 | QualifiedName[implicitCompletion=true] { identifier ("::" identifier)* } | 176 | ModuleName { QualifiedName } |
177 | |||
178 | QualifiedName[implicitCompletion=true] { "::"? identifier (QualifiedNameSeparator "::" identifier)* } | ||
171 | 179 | ||
172 | kw<term> { @specialize[@name={term},implicitCompletion=true]<identifier, term> } | 180 | kw<term> { @specialize[@name={term},implicitCompletion=true]<identifier, term> } |
173 | 181 | ||
@@ -181,6 +189,10 @@ sep1<separator, content> { content (separator content)* } | |||
181 | 189 | ||
182 | @skip { LineComment | BlockComment | whitespace } | 190 | @skip { LineComment | BlockComment | whitespace } |
183 | 191 | ||
192 | @external tokens qualifiedNameSeparator from "./tokens" { | ||
193 | QualifiedNameSeparator | ||
194 | } | ||
195 | |||
184 | @tokens { | 196 | @tokens { |
185 | whitespace { std.whitespace+ } | 197 | whitespace { std.whitespace+ } |
186 | 198 | ||
diff --git a/subprojects/frontend/src/language/problemLanguageSupport.ts b/subprojects/frontend/src/language/problemLanguageSupport.ts index 2121e05f..14826363 100644 --- a/subprojects/frontend/src/language/problemLanguageSupport.ts +++ b/subprojects/frontend/src/language/problemLanguageSupport.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | 2 | * SPDX-FileCopyrightText: 2021-2024 The Refinery Authors <https://refinery.tools/> |
3 | * | 3 | * |
4 | * SPDX-License-Identifier: EPL-2.0 | 4 | * SPDX-License-Identifier: EPL-2.0 |
5 | */ | 5 | */ |
@@ -27,7 +27,8 @@ const parserWithMetadata = parser.configure({ | |||
27 | styleTags({ | 27 | styleTags({ |
28 | LineComment: t.lineComment, | 28 | LineComment: t.lineComment, |
29 | BlockComment: t.blockComment, | 29 | BlockComment: t.blockComment, |
30 | 'problem class enum pred fn indiv scope': t.definitionKeyword, | 30 | 'module problem class enum pred fn scope': t.definitionKeyword, |
31 | 'import as declare atom multi': t.definitionKeyword, | ||
31 | 'abstract extends refers contains container opposite': t.modifier, | 32 | 'abstract extends refers contains container opposite': t.modifier, |
32 | 'default error contained containment': t.modifier, | 33 | 'default error contained containment': t.modifier, |
33 | 'true false unknown error': t.keyword, | 34 | 'true false unknown error': t.keyword, |
@@ -44,8 +45,9 @@ const parserWithMetadata = parser.configure({ | |||
44 | String: t.string, | 45 | String: t.string, |
45 | 'RelationName/QualifiedName': t.typeName, | 46 | 'RelationName/QualifiedName': t.typeName, |
46 | // 'RuleName/QualifiedName': t.typeName, | 47 | // 'RuleName/QualifiedName': t.typeName, |
47 | 'IndividualNodeName/QualifiedName': t.atom, | 48 | 'AtomNodeName/QualifiedName': t.atom, |
48 | 'VariableName/QualifiedName': t.variableName, | 49 | 'VariableName/QualifiedName': t.variableName, |
50 | 'ModuleName/QualifiedName': t.typeName, | ||
49 | '{ }': t.brace, | 51 | '{ }': t.brace, |
50 | '( )': t.paren, | 52 | '( )': t.paren, |
51 | '[ ]': t.squareBracket, | 53 | '[ ]': t.squareBracket, |
@@ -54,7 +56,8 @@ const parserWithMetadata = parser.configure({ | |||
54 | }), | 56 | }), |
55 | indentNodeProp.add({ | 57 | indentNodeProp.add({ |
56 | ProblemDeclaration: indentDeclaration, | 58 | ProblemDeclaration: indentDeclaration, |
57 | UniqueDeclaration: indentDeclaration, | 59 | AtomDeclaration: indentDeclaration, |
60 | NodeDeclaration: indentDeclaration, | ||
58 | ScopeDeclaration: indentDeclaration, | 61 | ScopeDeclaration: indentDeclaration, |
59 | PredicateBody: indentPredicateOrRule, | 62 | PredicateBody: indentPredicateOrRule, |
60 | FunctionBody: indentPredicateOrRule, | 63 | FunctionBody: indentPredicateOrRule, |
@@ -70,7 +73,8 @@ const parserWithMetadata = parser.configure({ | |||
70 | // RuleBody: foldInside, | 73 | // RuleBody: foldInside, |
71 | Conjunction: foldConjunction, | 74 | Conjunction: foldConjunction, |
72 | // Consequent: foldWholeNode, | 75 | // Consequent: foldWholeNode, |
73 | UniqueDeclaration: foldDeclaration, | 76 | AtomDeclaration: foldDeclaration, |
77 | NodeDeclaration: foldDeclaration, | ||
74 | ScopeDeclaration: foldDeclaration, | 78 | ScopeDeclaration: foldDeclaration, |
75 | BlockComment: foldBlockComment, | 79 | BlockComment: foldBlockComment, |
76 | }), | 80 | }), |
diff --git a/subprojects/frontend/src/language/tokens.ts b/subprojects/frontend/src/language/tokens.ts new file mode 100644 index 00000000..b5d022f1 --- /dev/null +++ b/subprojects/frontend/src/language/tokens.ts | |||
@@ -0,0 +1,46 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2018-2024 by Marijn Haverbeke <marijnh@gmail.com> and others | ||
3 | * Copyright (C) 2024 The Refinery Authors <https://refinery.tools/> | ||
4 | * | ||
5 | * SPDX-License-Identifier: MIT AND EPL-2.0 | ||
6 | * | ||
7 | * Based on the CSS tokenizer at | ||
8 | * https://github.com/lezer-parser/css/blob/790568c968a660a94bf0fbd97a86c66da1c529e5/src/tokens.js | ||
9 | */ | ||
10 | |||
11 | import { ExternalTokenizer } from '@lezer/lr'; | ||
12 | |||
13 | /* eslint-disable-next-line import/no-unresolved -- | ||
14 | Synthetic import from `@lezer/generator/rollup` cannot be found by ESLint. | ||
15 | */ | ||
16 | import { QualifiedNameSeparator } from './problem.grammar.terms'; | ||
17 | |||
18 | const colon = 58; | ||
19 | const underscore = 95; | ||
20 | |||
21 | function isAlpha(ch: number) { | ||
22 | return (ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) || ch >= 161; | ||
23 | } | ||
24 | |||
25 | function isDigit(ch: number) { | ||
26 | return ch >= 48 && ch <= 57; | ||
27 | } | ||
28 | |||
29 | function isIdentifier(ch: number) { | ||
30 | return isAlpha(ch) || isDigit(ch) || ch === underscore; | ||
31 | } | ||
32 | |||
33 | /* eslint-disable-next-line import/prefer-default-export -- | ||
34 | Lezer requires a named export. | ||
35 | */ | ||
36 | export const qualifiedNameSeparator = new ExternalTokenizer((input) => { | ||
37 | if (input.peek(0) === colon && input.peek(1) === colon) { | ||
38 | const previous = input.peek(-1); | ||
39 | if (isIdentifier(previous)) { | ||
40 | // Inject an extra 0-length token into the token stream to let Lezer | ||
41 | // consume the `::` on its own. Explicitly consuming 2 characters here | ||
42 | // leads to inconsistent highlighting of qualified names. | ||
43 | input.acceptToken(QualifiedNameSeparator); | ||
44 | } | ||
45 | } | ||
46 | }); | ||
diff --git a/subprojects/frontend/src/xtext/ContentAssistService.ts b/subprojects/frontend/src/xtext/ContentAssistService.ts index ac8ab36a..d42dde14 100644 --- a/subprojects/frontend/src/xtext/ContentAssistService.ts +++ b/subprojects/frontend/src/xtext/ContentAssistService.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | 2 | * SPDX-FileCopyrightText: 2021-2024 The Refinery Authors <https://refinery.tools/> |
3 | * | 3 | * |
4 | * SPDX-License-Identifier: EPL-2.0 | 4 | * SPDX-License-Identifier: EPL-2.0 |
5 | */ | 5 | */ |
@@ -108,7 +108,11 @@ function createCompletion(entry: ContentAssistEntry): Completion { | |||
108 | boost = -90; | 108 | boost = -90; |
109 | break; | 109 | break; |
110 | default: | 110 | default: |
111 | { | 111 | if (entry.proposal.startsWith('::')) { |
112 | // Move absolute names below relative names, | ||
113 | // they should only be preferred if no relative name is available. | ||
114 | boost = -60; | ||
115 | } else { | ||
112 | // Penalize qualified names (vs available unqualified names). | 116 | // Penalize qualified names (vs available unqualified names). |
113 | const extraSegments = entry.proposal.match(/::/g)?.length || 0; | 117 | const extraSegments = entry.proposal.match(/::/g)?.length || 0; |
114 | boost = Math.max(-5 * extraSegments, -50); | 118 | boost = Math.max(-5 * extraSegments, -50); |