Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
316 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// A recursive descent parser operates by defining functions for all | ||
// syntactic elements, and recursively calling those, each function | ||
// advancing the input stream and returning an AST node. Precedence | ||
// of constructs (for example, the fact that `!x[1]` means `!(x[1])` | ||
// instead of `(!x)[1]` is handled by the fact that the parser | ||
// function that parses unary prefix operators is called first, and | ||
// in turn calls the function that parses `[]` subscripts — that | ||
// way, it'll receive the node for `x[1]` already parsed, and wraps | ||
// *that* in the unary operator node. | ||
// | ||
// Acorn uses an [operator precedence parser][opp] to handle binary | ||
// operator precedence, because it is much more compact than using | ||
// the technique outlined above, which uses different, nesting | ||
// functions to specify precedence, for all of the ten binary | ||
// precedence levels that JavaScript defines. | ||
// | ||
// [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser | ||
|
||
use crate::node; | ||
use crate::parseutil; | ||
use crate::state; | ||
use crate::tokentype; | ||
|
||
pub trait ParserExpression { | ||
fn parseExpression( | ||
self, | ||
noIn: Option<bool>, | ||
refDestructuringErrors: Option<parseutil::DestructuringErrors>, | ||
) -> node::Node; | ||
} | ||
|
||
impl ParserExpression for state::Parser { | ||
fn parseExpression( | ||
self, | ||
noIn: Option<bool>, | ||
refDestructuringErrors: Option<parseutil::DestructuringErrors>, | ||
) -> node::Node { | ||
let startPos = self.pos; | ||
let startLoc = self.startLoc; | ||
let expr = self.maybeAssign(noIn, refDestructuringErrors); | ||
if self.r#type == tokentype::TokenType::comma() { | ||
let node = self.startNodeAt(startPos, startLoc); | ||
// TODO: finish this | ||
} | ||
expr | ||
} | ||
} |
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,18 @@ | ||
use crate::locutil; | ||
use crate::state; | ||
|
||
trait ParserLocation { | ||
fn curPosition(self) -> Option<locutil::Position>; | ||
} | ||
|
||
impl ParserLocation for state::Parser { | ||
fn curPosition(self) -> Option<locutil::Position> { | ||
if self.options.locations { | ||
return Some(locutil::Position::new( | ||
self.curLine, | ||
self.pos - self.lineStart, | ||
)); | ||
} | ||
None | ||
} | ||
} |
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
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,19 @@ | ||
pub struct DestructuringErrors { | ||
shorthandAssign: isize, | ||
trailingComma: isize, | ||
parenthesizedAssign: isize, | ||
parenthesizedBind: isize, | ||
doubleProto: isize, | ||
} | ||
|
||
impl DestructuringErrors { | ||
fn new() -> Self { | ||
DestructuringErrors { | ||
shorthandAssign: -1, | ||
trailingComma: -1, | ||
parenthesizedAssign: -1, | ||
parenthesizedBind: -1, | ||
doubleProto: -1, | ||
} | ||
} | ||
} |
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,46 @@ | ||
use crate::node; | ||
use crate::options; | ||
use crate::state; | ||
use crate::tokentype; | ||
|
||
use std::collections::HashMap; | ||
|
||
use node::ParserNode; | ||
|
||
// ### Statement parsing | ||
|
||
// Parse a program. Initializes the parser, reads any number of | ||
// statements, and wraps them in a Program node. Optionally takes a | ||
// `program` argument. If present, the statements will be appended | ||
// to its body instead of creating a new node. | ||
|
||
pub trait ParserStatement { | ||
fn parseTopLevel(&self, node: node::Node) -> node::Node; | ||
} | ||
|
||
impl ParserStatement for state::Parser { | ||
fn parseTopLevel(&self, node: node::Node) -> node::Node { | ||
let exports: HashMap<String, bool> = HashMap::new(); | ||
if node.body.is_none() { | ||
node.body = Some(Vec::new()); | ||
} | ||
while self.r#type != tokentype::TokenType::eof() { | ||
let stmt = self.parseStatement(None, true, exports); | ||
node.body.expect("").push(stmt); | ||
} | ||
if self.inModule { | ||
for name in self.undefinedExports.keys() { | ||
self.raiseRecoverable( | ||
self.undefinedExports[name].start, | ||
format!("Export #{} is not defined", name), | ||
); | ||
} | ||
} | ||
self.adaptDirectivePrologue(node.body); | ||
self.next(); | ||
if self.options.ecmaVersion >= options::EcmaVersion::Ecma6 { | ||
node.sourceType = Some(self.options.sourceType); | ||
} | ||
self.finishNode(node, String::from("Program")) | ||
} | ||
} |
Oops, something went wrong.