1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use joker::token::{Token, TokenData};
use joker::word::Reserved;
use context::LabelType;
pub trait First {
fn first_binding(&self) -> bool;
}
pub trait Follows {
fn follow_statement_list(&self) -> bool;
}
impl First for Token {
// first(LexicalBinding) =
// first(BindingIdentifier)
// U first(BindingPattern)
// = IdentifierName
// U first(BindingPattern)
// = IdentifierName
// U first(ObjectBindingPattern)
// U first(ArrayBindingPattern)
// = IdentifierName U { '{', '[' }
fn first_binding(&self) -> bool {
match self.value {
TokenData::LBrace
| TokenData::LBrack
| TokenData::Identifier(_) => true,
_ => false
}
}
}
impl Follows for Token {
// follow(StatementList) =
// follow(CaseClause)
// U follow(DefaultClause)
// U follow(FunctionBody)
// U follow(ScriptBody)
// U follow(ModuleBody)
// U { '}' }
// = { '}', 'case', 'default', EOF }
//
// follow(CaseClause) =
// { '}' }
// U first(CaseClause)
// U first(DefaultClause)
// = { '}', 'case', 'default' }
//
// follow(DefaultClause) =
// { '}' }
// U first(CaseClause)
// = { '}', 'case' }
//
// first(CaseClause) = { 'case' }
// first(DefaultClause) = { 'default' }
//
// follow(ScriptBody) = { EOF }
// follow(ModuleBody) = { EOF }
fn follow_statement_list(&self) -> bool {
match self.value {
TokenData::Reserved(Reserved::Case)
| TokenData::Reserved(Reserved::Default)
| TokenData::EOF
| TokenData::RBrace => true,
_ => false
}
}
}
pub trait HasLabelType {
fn label_type(&self) -> LabelType;
}
impl HasLabelType for Token {
fn label_type(&self) -> LabelType {
match self.value {
TokenData::Reserved(Reserved::Do)
| TokenData::Reserved(Reserved::While)
| TokenData::Reserved(Reserved::For) => LabelType::Iteration,
_ => LabelType::Statement
}
}
}