mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 06:58:11 +00:00
LibJS: Add Javascript lexer and parser
This adds a basic Javascript lexer and parser. It can parse the currently existing demo programs. More work needs to be done to turn it into a complete parser than can parse arbitrary JS Code. The lexer outputs tokens with preceeding whitespace and comments in the trivia member. This should allow us to generate the exact source code by concatenating the generated tokens. The parser is written in a way that it always returns a complete syntax tree. Error conditions are represented as nodes in the tree. This simplifies the code and allows it to be used as an early stage parser, e.g for parsing JS documents in an IDE while editing the source code.:
This commit is contained in:
parent
17705d23fb
commit
f3a9eba987
11 changed files with 1069 additions and 36 deletions
|
@ -44,6 +44,11 @@ Value FunctionDeclaration::execute(Interpreter& interpreter) const
|
|||
return Value(function);
|
||||
}
|
||||
|
||||
Value ExpressionStatement::execute(Interpreter& interpreter) const
|
||||
{
|
||||
return m_expression->execute(interpreter);
|
||||
}
|
||||
|
||||
Value CallExpression::execute(Interpreter& interpreter) const
|
||||
{
|
||||
if (name() == "$gc") {
|
||||
|
@ -61,7 +66,7 @@ Value CallExpression::execute(Interpreter& interpreter) const
|
|||
|
||||
Value ReturnStatement::execute(Interpreter& interpreter) const
|
||||
{
|
||||
auto value = argument().execute(interpreter);
|
||||
auto value = argument() ? argument()->execute(interpreter) : js_undefined();
|
||||
interpreter.do_return();
|
||||
return value;
|
||||
}
|
||||
|
@ -310,7 +315,8 @@ void FunctionDeclaration::dump(int indent) const
|
|||
void ReturnStatement::dump(int indent) const
|
||||
{
|
||||
ASTNode::dump(indent);
|
||||
argument().dump(indent + 1);
|
||||
if (argument())
|
||||
argument()->dump(indent + 1);
|
||||
}
|
||||
|
||||
void IfStatement::dump(int indent) const
|
||||
|
@ -413,6 +419,12 @@ void ObjectExpression::dump(int indent) const
|
|||
ASTNode::dump(indent);
|
||||
}
|
||||
|
||||
void ExpressionStatement::dump(int indent) const
|
||||
{
|
||||
ASTNode::dump(indent);
|
||||
m_expression->dump(indent + 1);
|
||||
}
|
||||
|
||||
Value ObjectExpression::execute(Interpreter& interpreter) const
|
||||
{
|
||||
return Value(interpreter.heap().allocate<Object>());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue