-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the beginnings of the new parser
- Loading branch information
1 parent
0ef3b94
commit a8414fb
Showing
9 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* parser.hpp | ||
* | ||
* Created on: Jun 22, 2019 | ||
* Author: iconmaster | ||
*/ | ||
|
||
#ifndef INCLUDE_CCL_PARSER_HPP_ | ||
#define INCLUDE_CCL_PARSER_HPP_ | ||
|
||
#include "ccl/types.hpp" | ||
#include "ccl/lexer.hpp" | ||
#include "ccl/program.hpp" | ||
|
||
namespace ccl { | ||
Program parse(Context& ctx, Lexer& lexer); | ||
|
||
template<typename... Args> | ||
inline Program parse(Context& ctx, Args... args) { | ||
Lexer lexer{args...}; | ||
return parse(ctx, lexer); | ||
} | ||
} | ||
|
||
#endif /* INCLUDE_CCL_PARSER_HPP_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* parser.cpp | ||
* | ||
* Created on: Jun 22, 2019 | ||
* Author: iconmaster | ||
*/ | ||
|
||
#include "ccl/parser.hpp" | ||
#include "ccl/classes.hpp" | ||
|
||
using namespace ccl; | ||
using namespace std; | ||
|
||
static void parsePostConst(Context& ctx, Lexer& lexer) { | ||
Token& t = lexer.peek(); | ||
switch (t.type) { | ||
case Token::Type::EX_STRING: | ||
case Token::Type::FLAG: | ||
case Token::Type::LBRACE: | ||
case Token::Type::LBRACKET: | ||
case Token::Type::LPAREN: | ||
case Token::Type::STRING: | ||
case Token::Type::VAR: | ||
case Token::Type::WORD: | ||
throw Error("Unexpected token after constant: " + t.value); | ||
} | ||
} | ||
|
||
static optional<Program> parseExprPart(Context& ctx, Lexer& lexer) { | ||
Token t = lexer.peek(); | ||
switch (t.type) { | ||
case Token::Type::VAR: { | ||
lexer.next(); | ||
auto p = (Program) ProgramVar(t.value, t.source); | ||
parsePostConst(ctx, lexer); | ||
return optional<Program>(p); | ||
} break; | ||
default: | ||
return optional<Program>(); | ||
} | ||
} | ||
|
||
static Program parseExpr(Context& ctx, Lexer& lexer) { | ||
auto p = parseExprPart(ctx, lexer); | ||
bool done = false; | ||
while (!done) { | ||
Token t = lexer.peek(); | ||
switch (t.type) { | ||
case Token::Type::PIPE: { | ||
lexer.next(); | ||
if (!p) p = (Program) ProgramNull{t.source}; | ||
auto rhs = parseExprPart(ctx, lexer); | ||
if (!rhs) rhs = (Program) ProgramNull{t.source}; | ||
p = (Program) ProgramPipe(*p, *rhs, t.source); | ||
} break; | ||
case Token::Type::SEMICOLON: { | ||
lexer.next(); | ||
if (!p) p = (Program) ProgramNull{t.source}; | ||
auto rhs = parseExprPart(ctx, lexer); | ||
if (!rhs) rhs = (Program) ProgramNull{t.source}; | ||
p = (Program) ProgramSequence(*p, *rhs, t.source); | ||
} break; | ||
case Token::Type::NONE: | ||
case Token::Type::RPAREN: | ||
case Token::Type::RBRACE: { | ||
if (!p) p = (Program) ProgramNull{t.source}; | ||
done = true; | ||
} break; | ||
default: | ||
done = true; | ||
} | ||
} | ||
|
||
if (p) { | ||
return *p; | ||
} | ||
|
||
throw Error("Unexpected token in expression: " + lexer.peek().value); | ||
} | ||
|
||
Program ccl::parse(Context& ctx, Lexer& lexer) { | ||
return parseExpr(ctx, lexer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* test-parser.cpp | ||
* | ||
* Created on: Jun 22, 2019 | ||
* Author: iconmaster | ||
*/ | ||
|
||
#define BOOST_TEST_DYN_LINK | ||
#include <boost/test/unit_test.hpp> | ||
|
||
#include <vector> | ||
|
||
#include "ccl/parser.hpp" | ||
#include "ccl/context.hpp" | ||
|
||
using namespace std; | ||
using namespace ccl; | ||
|
||
static string location = "TEST LOCATION"; | ||
|
||
namespace std { | ||
static ostream& operator<<(ostream& os, const Token::Type t) { | ||
os << (void*) t; | ||
return os; | ||
} | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE(parser); | ||
|
||
BOOST_AUTO_TEST_CASE(aParse) { | ||
Context ctx; | ||
|
||
BOOST_CHECK_NO_THROW({ | ||
Program p = parse(ctx, location, "$x | $y ; $z"); | ||
}); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END(); |