Some enhancements in the parser
- Fix MemberExpression and IfStatementPart not receiving annotations - Enable parsing of elif-statements - Refactor some code
This commit is contained in:
parent
2be3deb5f6
commit
a3f40d51ef
2 changed files with 42 additions and 36 deletions
|
@ -67,7 +67,7 @@ namespace bolt {
|
||||||
|
|
||||||
OperatorTable ExprOperators;
|
OperatorTable ExprOperators;
|
||||||
|
|
||||||
Token* peekFirstTokenAfterModifiers();
|
Token* peekFirstTokenAfterAnnotationsAndModifiers();
|
||||||
|
|
||||||
Token* expectToken(NodeKind Ty);
|
Token* expectToken(NodeKind Ty);
|
||||||
|
|
||||||
|
|
|
@ -78,7 +78,7 @@ namespace bolt {
|
||||||
ExprOperators.add("$", OperatorFlags_InfixR, 0);
|
ExprOperators.add("$", OperatorFlags_InfixR, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Token* Parser::peekFirstTokenAfterModifiers() {
|
Token* Parser::peekFirstTokenAfterAnnotationsAndModifiers() {
|
||||||
std::size_t I = 0;
|
std::size_t I = 0;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
auto T0 = Tokens.peek(I++);
|
auto T0 = Tokens.peek(I++);
|
||||||
|
@ -737,11 +737,14 @@ after_tuple_elements:
|
||||||
switch (T2->getKind()) {
|
switch (T2->getKind()) {
|
||||||
case NodeKind::IntegerLiteral:
|
case NodeKind::IntegerLiteral:
|
||||||
case NodeKind::Identifier:
|
case NodeKind::Identifier:
|
||||||
|
{
|
||||||
Tokens.get();
|
Tokens.get();
|
||||||
Tokens.get();
|
Tokens.get();
|
||||||
E = new MemberExpression { E->Annotations, E, static_cast<Dot*>(T1), T2 };
|
auto Annotations = E->Annotations;
|
||||||
E->Annotations.clear();
|
E->Annotations = {};
|
||||||
|
E = new MemberExpression { Annotations, E, static_cast<Dot*>(T1), T2 };
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
@ -780,7 +783,7 @@ finish:
|
||||||
return Operator;
|
return Operator;
|
||||||
}
|
}
|
||||||
auto Annotations = Operator->Annotations;
|
auto Annotations = Operator->Annotations;
|
||||||
Operator->Annotations.clear();
|
Operator->Annotations = {};
|
||||||
return new CallExpression(Annotations, Operator, Args);
|
return new CallExpression(Annotations, Operator, Args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -884,6 +887,7 @@ finish:
|
||||||
|
|
||||||
IfStatement* Parser::parseIfStatement() {
|
IfStatement* Parser::parseIfStatement() {
|
||||||
std::vector<IfStatementPart*> Parts;
|
std::vector<IfStatementPart*> Parts;
|
||||||
|
auto Annotations = parseAnnotations();
|
||||||
auto IfKeyword = expectToken<class IfKeyword>();
|
auto IfKeyword = expectToken<class IfKeyword>();
|
||||||
auto Test = parseExpression();
|
auto Test = parseExpression();
|
||||||
if (!Test) {
|
if (!Test) {
|
||||||
|
@ -911,39 +915,41 @@ finish:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Tokens.get()->unref(); // Always a LineFoldEnd
|
Tokens.get()->unref(); // Always a LineFoldEnd
|
||||||
Parts.push_back(new IfStatementPart(IfKeyword, Test, T1, Then));
|
Parts.push_back(new IfStatementPart(Annotations, IfKeyword, Test, T1, Then));
|
||||||
for (;;) {
|
for (;;) {
|
||||||
auto T3 = Tokens.peek();
|
auto T3 = peekFirstTokenAfterAnnotationsAndModifiers();
|
||||||
if (T3->getKind() == NodeKind::ElseKeyword || T3->getKind() == NodeKind::ElifKeyword) {
|
if (T3->getKind() != NodeKind::ElseKeyword && T3->getKind() != NodeKind::ElifKeyword) {
|
||||||
Tokens.get();
|
break;
|
||||||
Expression* Test = nullptr;
|
}
|
||||||
if (T3->getKind() == NodeKind::ElifKeyword) {
|
auto Annotations = parseAnnotations();
|
||||||
Test = parseExpression();
|
Tokens.get();
|
||||||
|
Expression* Test = nullptr;
|
||||||
|
if (T3->getKind() == NodeKind::ElifKeyword) {
|
||||||
|
Test = parseExpression();
|
||||||
|
}
|
||||||
|
auto T4 = expectToken<BlockStart>();
|
||||||
|
if (!T4) {
|
||||||
|
for (auto Part: Parts) {
|
||||||
|
Part->unref();
|
||||||
}
|
}
|
||||||
auto T4 = expectToken<BlockStart>();
|
return nullptr;
|
||||||
if (!T4) {
|
}
|
||||||
for (auto Part: Parts) {
|
std::vector<Node*> Alt;
|
||||||
Part->unref();
|
for (;;) {
|
||||||
}
|
auto T5 = Tokens.peek();
|
||||||
return nullptr;
|
if (T5->getKind() == NodeKind::BlockEnd) {
|
||||||
}
|
Tokens.get()->unref();
|
||||||
std::vector<Node*> Alt;
|
|
||||||
for (;;) {
|
|
||||||
auto T5 = Tokens.peek();
|
|
||||||
if (T5->getKind() == NodeKind::BlockEnd) {
|
|
||||||
Tokens.get()->unref();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
auto Element = parseLetBodyElement();
|
|
||||||
if (Element) {
|
|
||||||
Alt.push_back(Element);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Tokens.get()->unref(); // Always a LineFoldEnd
|
|
||||||
Parts.push_back(new IfStatementPart(T3, Test, T4, Alt));
|
|
||||||
if (T3->getKind() == NodeKind::ElseKeyword) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
auto Element = parseLetBodyElement();
|
||||||
|
if (Element) {
|
||||||
|
Alt.push_back(Element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Tokens.get()->unref(); // Always a LineFoldEnd
|
||||||
|
Parts.push_back(new IfStatementPart(Annotations, T3, Test, T4, Alt));
|
||||||
|
if (T3->getKind() == NodeKind::ElseKeyword) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new IfStatement(Parts);
|
return new IfStatement(Parts);
|
||||||
|
@ -1097,7 +1103,7 @@ finish:
|
||||||
}
|
}
|
||||||
|
|
||||||
Node* Parser::parseLetBodyElement() {
|
Node* Parser::parseLetBodyElement() {
|
||||||
auto T0 = peekFirstTokenAfterModifiers();
|
auto T0 = peekFirstTokenAfterAnnotationsAndModifiers();
|
||||||
switch (T0->getKind()) {
|
switch (T0->getKind()) {
|
||||||
case NodeKind::LetKeyword:
|
case NodeKind::LetKeyword:
|
||||||
return parseLetDeclaration();
|
return parseLetDeclaration();
|
||||||
|
@ -1516,7 +1522,7 @@ next_member:
|
||||||
}
|
}
|
||||||
|
|
||||||
Node* Parser::parseSourceElement() {
|
Node* Parser::parseSourceElement() {
|
||||||
auto T0 = peekFirstTokenAfterModifiers();
|
auto T0 = peekFirstTokenAfterAnnotationsAndModifiers();
|
||||||
switch (T0->getKind()) {
|
switch (T0->getKind()) {
|
||||||
case NodeKind::LetKeyword:
|
case NodeKind::LetKeyword:
|
||||||
return parseLetDeclaration();
|
return parseLetDeclaration();
|
||||||
|
|
Loading…
Reference in a new issue