-
Notifications
You must be signed in to change notification settings - Fork 1
/
TinyComp.ts
72 lines (66 loc) · 2.35 KB
/
TinyComp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// deno-lint-ignore-file
// ##################################################################### //
// ############################## TinyComp ############################# //
// ##################################################################### //
import AttributeGrammar from "./attributeGrammar/attributeGrammar.ts";
import LexicalRuleset from "./attributeGrammar/lexicalRuleset.ts";
import SyntaxRuleset from "./attributeGrammar/syntaxRuleset.ts";
import SemanticContext from "./codeGenerator/SemanticContext.ts";
import Attribute from "./codeGenerator/Attribute.ts";
import {
_getFirstSemanticContextBySyntaxRuleName,
_getSemanticContextsBySyntaxRuleName,
} from "./attributeGrammar/semanticRuleset.ts";
import SemanticRuleset from "./attributeGrammar/semanticRuleset.ts";
import Lexer from "./lexer/Lexer.ts";
import Parser from "./parser/Parser.ts";
import CodeGenerator from "./codeGenerator/CodeGenerator.ts";
import Token from "./lexer/Token.ts";
import SyntaxRule from "./parser/SyntaxRule.ts";
// Interface that describes extra options for the compiler
interface TinyCompOptions {
startSymbol: string; // the start symbol of the grammar (root of the syntax tree)
ignoreTokensNamed?: string[]; // the names of the tokens that should be ignored during parsing
}
export default class TinyComp {
lexer: Lexer;
parser: Parser;
codeGenerator: CodeGenerator;
compilerOptions: TinyCompOptions;
constructor(
attributeGrammar: AttributeGrammar,
compilerOptions: TinyCompOptions
) {
this.compilerOptions = compilerOptions;
this.lexer = new Lexer(attributeGrammar.lexicalRuleset);
this.parser = new Parser(
attributeGrammar.syntaxRuleset,
compilerOptions.startSymbol,
compilerOptions.ignoreTokensNamed ?? []
);
this.codeGenerator = new CodeGenerator(attributeGrammar.semanticRuleset);
}
compile(input: string): any {
const tokens = this.lexer.tokenize(input);
const syntaxParseTree = this.parser.parse(
tokens,
this.compilerOptions.startSymbol
);
Token.numOfTokens = 0;
SyntaxRule.stack = [];
return this.codeGenerator.generate(syntaxParseTree);
}
}
export type {
AttributeGrammar,
LexicalRuleset,
SyntaxRuleset,
SemanticRuleset,
TinyCompOptions,
};
export {
_getFirstSemanticContextBySyntaxRuleName,
_getSemanticContextsBySyntaxRuleName,
SemanticContext,
Attribute,
};