Skip to content

Commit ba5d85f

Browse files
committed
change rule statements to use dictionary
1 parent e341285 commit ba5d85f

File tree

1 file changed

+10
-34
lines changed

1 file changed

+10
-34
lines changed

src/ast.ts

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
11
import { BraceExpression, CompileStatement, CompilerError, ExportStatement, Expression, FunctionStatement, ImportsStatement, KeywordStatement, Node, NodeType, OperatorStatement, ParenExpression, PrimitiveTypeExpression, ProgramStatement, SquareExpression, StringExpression, Token, TokenType, VariableExpression } from './types.js';
22
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';
284

295

306
export namespace syxparser {
@@ -216,28 +192,28 @@ export namespace syxparser {
216192
return node({ type: NodeType.Keyword, word: ex.value, range: combineTwo(token, ex.range) }, put);
217193
} else if (token.type === TokenType.RuleKeyword) {
218194
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\'.'); }
220196
if (at().value !== ':') throw new CompilerError(at().range, 'Expected \':\' after rule name.');
221197
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);
224200

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}'.`); }
228204

229205

230206
if (at().type !== TokenType.Semicolon) throw new CompilerError(at().range, 'Expected semicolon after rule statement.');
231207
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;
234210
if (!(
235211
keyEx.type === NodeType.String &&
236212
program.body.some(s =>
237213
(s.type === NodeType.Keyword && (s as KeywordStatement).word === keyEx.value) ||
238214
(s.type === NodeType.Export && (s as ExportStatement).body.type === NodeType.Keyword && ((s as ExportStatement).body as KeywordStatement).word === keyEx.value)
239215
)
240-
)) throw new CompilerError(keyEx.range, 'Unknown keyword.');
216+
)) throw new CompilerError(keyEx.range, `Can't find keyword ${keyEx.value}.`);
241217

242218
if (at().type !== TokenType.Semicolon) throw new CompilerError(at().range, 'Expected semicolon after rule statement.');
243219
return node({ type: NodeType.Rule, rule: ruleExpr.value, value: keyEx.value, range: combineTwo(token, tokens.shift()) }, put);

0 commit comments

Comments
 (0)