|
1 | 1 | import { BraceExpression, CompileStatement, CompilerError, ExportStatement, Expression, FunctionStatement, ImportsStatement, KeywordStatement, Node, NodeType, OperatorStatement, ParenExpression, PrimitiveTypeExpression, ProgramStatement, SquareExpression, StringExpression, Token, TokenType, VariableExpression } from './types.js'; |
2 | 2 | import { Range } from './diagnosticTypes.js'; |
3 | | - |
4 | | -const valueTypeDefinitions = { |
5 | | - boolean: { value: 'boolean', regex: /^(true|false)$/ }, |
6 | | - keyword: { value: 'keyword' } |
7 | | -}; |
8 | | - |
9 | | -export const SyxRuleRegistry: Record<string, { value: string, regex?: RegExp; }> = { |
10 | | - |
11 | | - /** |
12 | | - * Determines whether it is possible to return a value using functons. |
13 | | - * @author efekos |
14 | | - * @version 1.0.0 |
15 | | - * @since 0.0.1-alpha |
16 | | - */ |
17 | | - 'function-value-return-enabled': valueTypeDefinitions.boolean, |
18 | | - |
19 | | - /** |
20 | | - * Determines the keyword that should be used to return values from a function, similiar to `return` keyword |
21 | | - * from popular languages such as ts,js,py,java etc. |
22 | | - * @author efekos |
23 | | - * @version 1.0.0 |
24 | | - * @since 0.0.1-alpha |
25 | | - */ |
26 | | - 'function-value-return-keyword': valueTypeDefinitions.keyword |
27 | | -}; |
| 3 | +import { dictionary } from './dictionary/dictionary.js'; |
28 | 4 |
|
29 | 5 |
|
30 | 6 | export namespace syxparser { |
@@ -216,28 +192,28 @@ export namespace syxparser { |
216 | 192 | return node({ type: NodeType.Keyword, word: ex.value, range: combineTwo(token, ex.range) }, put); |
217 | 193 | } else if (token.type === TokenType.RuleKeyword) { |
218 | 194 | const ruleExpr = parseExpression(false, false); |
219 | | - if (ruleExpr.type !== NodeType.String) { throw new CompilerError(ruleExpr.range, 'Expected string after \'rule\'.'); } |
| 195 | + if (ruleExpr.type !== NodeType.String) { throw new CompilerError(ruleExpr.range, 'Expected rule name as string after \'rule\'.'); } |
220 | 196 | if (at().value !== ':') throw new CompilerError(at().range, 'Expected \':\' after rule name.'); |
221 | 197 | tokens.shift(); |
222 | | - if (!(ruleExpr.value in SyxRuleRegistry)) throw new CompilerError(ruleExpr.range, `Unknown rule '${ruleExpr.value}'.`); |
223 | | - const rule = SyxRuleRegistry[ruleExpr.value]; |
| 198 | + if (!dictionary.Rules.find(r=>r.name===ruleExpr.value)) throw new CompilerError(ruleExpr.range, `Unknown rule '${ruleExpr.value}'.`); |
| 199 | + const rule = dictionary.Rules.find(r=>r.name===ruleExpr.value); |
224 | 200 |
|
225 | | - if (rule.value === 'boolean') { |
226 | | - const boolEx = parseExpression(false, false, true); |
227 | | - if (!(boolEx.type === NodeType.String && rule.regex.test(boolEx.value))) { throw new CompilerError(boolEx.range, 'Expected boolean as rule value.'); } |
| 201 | + if (rule.type === 'boolean') { |
| 202 | + const boolEx = parseExpression(false, false, true) as Expression; |
| 203 | + if (!(boolEx.type === NodeType.String && dictionary.RuleTypeRegexes.boolean.test(boolEx.value))) { throw new CompilerError(boolEx.range, `Rule '${rule.name}' requires a boolean value, found '${boolEx.value}'.`); } |
228 | 204 |
|
229 | 205 |
|
230 | 206 | if (at().type !== TokenType.Semicolon) throw new CompilerError(at().range, 'Expected semicolon after rule statement.'); |
231 | 207 | return node({ type: NodeType.Rule, rule: ruleExpr.value, value: boolEx.value, range: combineTwo(token, tokens.shift()) }, put); |
232 | | - } else if (rule.value === 'keyword') { |
233 | | - const keyEx = parseExpression(false, false, true); |
| 208 | + } else if (rule.type === 'keyword') { |
| 209 | + const keyEx = parseExpression(false, false, true) as Expression; |
234 | 210 | if (!( |
235 | 211 | keyEx.type === NodeType.String && |
236 | 212 | program.body.some(s => |
237 | 213 | (s.type === NodeType.Keyword && (s as KeywordStatement).word === keyEx.value) || |
238 | 214 | (s.type === NodeType.Export && (s as ExportStatement).body.type === NodeType.Keyword && ((s as ExportStatement).body as KeywordStatement).word === keyEx.value) |
239 | 215 | ) |
240 | | - )) throw new CompilerError(keyEx.range, 'Unknown keyword.'); |
| 216 | + )) throw new CompilerError(keyEx.range, `Can't find keyword ${keyEx.value}.`); |
241 | 217 |
|
242 | 218 | if (at().type !== TokenType.Semicolon) throw new CompilerError(at().range, 'Expected semicolon after rule statement.'); |
243 | 219 | return node({ type: NodeType.Rule, rule: ruleExpr.value, value: keyEx.value, range: combineTwo(token, tokens.shift()) }, put); |
|
0 commit comments