parser experiments

This commit is contained in:
Vadim Lopatin 2016-09-12 17:45:45 +03:00
parent 420812aa3c
commit d8145233c1
2 changed files with 70 additions and 14 deletions

View File

@ -5,23 +5,57 @@ import ddc.lexer.ast;
import dlangui.core.textsource; import dlangui.core.textsource;
import dlangui.core.logger; import dlangui.core.logger;
ASTNode parseSource(dstring text, SourceFile file) { class Parser {
ASTNode res; SourceLines _lines;
SourceFile _file;
Token[] _tokens;
int[] _pairedBracket;
int[] _bracketLevel;
void init(SourceLines lines, SourceFile file) {
_lines = lines;
_file = file;
}
void init(dstring text, SourceFile file) {
import std.array; import std.array;
ArraySourceLines lines = new ArraySourceLines(); ArraySourceLines lines = new ArraySourceLines();
dstring[] src = text.split('\n'); dstring[] src = text.split('\n');
lines.initialize(src, file, 0); lines.initialize(src, file, 0);
Tokenizer tokenizer = new Tokenizer(lines); init(lines, file);
}
void init(dstring src, string filename) {
init(src, new SourceFile(filename));
}
bool findBracketPairs() {
bool res = true;
_pairedBracket.length = _tokens.length;
_pairedBracket[0 .. $] = -1;
_bracketLevel.length = _tokens.length;
_bracketLevel[0 .. $] = -1;
return res;
}
bool tokenize() {
bool res = false;
Tokenizer tokenizer = new Tokenizer(_lines);
//tokenizer.errorTolerant = true; //tokenizer.errorTolerant = true;
try { try {
Token[] tokens = tokenizer.allTokens(); _tokens = tokenizer.allTokens();
ulong len = tokens.length; Log.v("tokens: ", _tokens);
Log.v("tokens: ", tokens); findBracketPairs();
res = true;
} catch (Exception e) { } catch (Exception e) {
// error // error
Log.e("Tokenizer exception"); Log.e("Tokenizer exception", e);
} }
return res; return res;
}
}
ASTNode parseSource(dstring text, SourceFile file) {
ASTNode res;
Parser parser = new Parser();
parser.init(text, file);
parser.tokenize();
return res;
} }
ASTNode parseSource(dstring text, string filename) { ASTNode parseSource(dstring text, string filename) {

View File

@ -924,6 +924,28 @@ class Token {
@property float floatValue() { return 0; } @property float floatValue() { return 0; }
@property byte precision() { return 0; } @property byte precision() { return 0; }
@property bool isImaginary() { return false; } @property bool isImaginary() { return false; }
@property bool isBracket() {
OpCode op = opCode;
return op == OpCode.PAR_OPEN
|| op == OpCode.PAR_CLOSE
|| op == OpCode.SQ_OPEN
|| op == OpCode.SQ_CLOSE
|| op == OpCode.CURL_OPEN
|| op == OpCode.CURL_CLOSE;
}
@property bool isOpenBracket() {
OpCode op = opCode;
return op == OpCode.PAR_OPEN
|| op == OpCode.SQ_OPEN
|| op == OpCode.CURL_OPEN;
}
@property bool isCloseBracket() {
OpCode op = opCode;
return op == OpCode.PAR_CLOSE
|| op == OpCode.SQ_CLOSE
|| op == OpCode.CURL_CLOSE;
}
@property bool isEof() { return type == TokenType.EOF; }
/// returns opcode ID - for opcode tokens /// returns opcode ID - for opcode tokens
@property OpCode opCode() { return OpCode.NONE; } @property OpCode opCode() { return OpCode.NONE; }