Newer
Older
use crate::lexer::ScriptTokenType;
use crate::parser::ast::*;
use crate::runtime::numbers::Number;
grammar<'a>;

Louis
committed
<ExpressionList> => Program(<>),
}
ExpressionBlock: ExpressionList = {
"{" "}" => ExpressionList::empty(),
"{" <ExpressionList> "}" => <>,
}
ExpressionList: ExpressionList = {
<Expression> => ExpressionList::production(vec![<>]),
<mut list:ExpressionList> ";" <expr:Expression> => {
list.expressions.push(expr);
list
},
<mut opt:ExpressionList> ";" => {
opt.is_void = true;
opt
},
}

Louis
committed
FunctionDeclParamList = CommaSeperatedList<FunctionDeclParam>;
FunctionDeclParam: DeclareIdent = {
Identifier => DeclareIdent::WithoutValue(<>),
<id:Identifier> "=" <def:ValueExpression> => DeclareIdent::WithValue(Assignment {
ident: id,
value: Box::new(def),
}),
}

Louis
committed
ParameterList: Vec<ValueExpression> = {
"(" <CommaSeperatedValueExpressionList> ")" => <>,
"(" ")" => Vec::new(),
}

Louis
committed
CommaSeperatedValueExpressionList = CommaSeperatedList<ValueExpression>;

Louis
committed
ValueExpression => Expression::Value(<>),
VoidExpression => Expression::Void(<>),
}
ValueExpression: ValueExpression = {

Louis
committed
FirstClassDeclareExpression => <>,

Louis
committed
"let" <Identifier> => DeclareIdent::WithoutValue(<>).into(),

Louis
committed
"let" <id:Identifier> "=" <expr:FirstClassDeclareExpression> => DeclareIdent::WithValue(

Louis
committed
Assignment { ident: id, value :Box::new(expr) }
).into(),

Louis
committed
VoidExpression: VoidExpression = {
UnaryStatement => <>

Louis
committed
FirstClassDeclareExpression: ValueExpression = {
BoolOrExpression => <>,
DeclareFunction => <>.into(),
}
BoolOrExpression: ValueExpression = {

Louis
committed
BoolAndExpression => <>,
<lhs:BoolOrExpression> "||" <rhs:BoolAndExpression> => ValueExpression::bool_or(lhs, rhs),
BoolAndExpression: ValueExpression = {

Louis
committed
EqualityExpression => <>,
<lhs:BoolAndExpression> "&&" <rhs:EqualityExpression> => ValueExpression::bool_and(lhs, rhs),
EqualityExpression: ValueExpression = {

Louis
committed
ComparisonExpression => <>,
<lhs:EqualityExpression> "==" <rhs:ComparisonExpression> => ValueExpression::equals(lhs, rhs),
<lhs:EqualityExpression> "!=" <rhs:ComparisonExpression> => ValueExpression::not_equals(lhs, rhs),
ComparisonExpression: ValueExpression = {

Louis
committed
AdditiveExpression => <>,
<lhs:ComparisonExpression> "<" <rhs:AdditiveExpression> => ValueExpression::less_than(lhs, rhs),
<lhs:ComparisonExpression> ">" <rhs:AdditiveExpression> => ValueExpression::greater_than(lhs, rhs),
<lhs:ComparisonExpression> "<=" <rhs:AdditiveExpression> => ValueExpression::greater_than_equal(lhs, rhs),
<lhs:ComparisonExpression> ">=" <rhs:AdditiveExpression> => ValueExpression::greater_than_equal(lhs, rhs),
AdditiveExpression: ValueExpression = {

Louis
committed
MultiplicativeExpression => <>,
<lhs:AdditiveExpression> "+" <rhs:MultiplicativeExpression> => ValueExpression::add(lhs, rhs),
<lhs:AdditiveExpression> "-" <rhs:MultiplicativeExpression> => ValueExpression::sub(lhs, rhs),
MultiplicativeExpression: ValueExpression = {

Louis
committed
UnaryExpression => <>,
<lhs:MultiplicativeExpression> "*" <rhs:UnaryExpression> => ValueExpression::mul(lhs, rhs),
<lhs:MultiplicativeExpression> "/" <rhs:UnaryExpression> => ValueExpression::div(lhs, rhs),
<lhs:MultiplicativeExpression> "%" <rhs:UnaryExpression> => ValueExpression::modulo(lhs, rhs),
}

Louis
committed
UnaryStatement: VoidExpression = {

Louis
committed
"print" <FirstClassDeclareExpression> => Print { expr: Box::new(<>) }.into(),
"return" <FirstClassDeclareExpression> => Return { expr: Box::new(<>) }.into(),

Louis
committed
}
UnaryExpression: ValueExpression = {

Louis
committed
IndexedExpression => <>,
"typeof" <UnaryExpression> => TypeofValue(Box::new(<>)).into(),
"!" <UnaryExpression> => ValueExpression::Unary { operand: Box::new(<>), operator: UnaryOp::Not },
"-" <UnaryExpression> => ValueExpression::Unary { operand: Box::new(<>), operator: UnaryOp::Negate },
}
IndexedExpression: ValueExpression = {

Louis
committed
CallExpression => <>,
<init:CallExpression> "." <acc:AccessorChainAnyEnding> => AccessorPath {
initialiser: Box::new(init),
path: acc,
}.into(),
}
CallExpression: ValueExpression = {
BaseExpression => <>,
FunctionCall => <>.into(),
BaseExpression: ValueExpression = {

Louis
committed
Literal => <>.into(),
Identifier => <>.into(),
Conditional => <>,
"(" <ValueExpression> ")" => GroupedExpression { inner: Box::new(<>) }.into(),
}

Louis
committed
DeclareFunction: DeclareFunction = {
"fn" <ident:Identifier> "(" <params:FunctionDeclParamList?> ")" <body:ExpressionBlock> => DeclareFunction {
ident,
params: params.unwrap_or_default(),
body,
}
}

Louis
committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
AccessorChainAnyEnding: Vec<AccessorType> = {
AccessorChainEndingWithProperty => <>,
FunctionCall => vec![AccessorType::FunctionCall(<>)],
<mut ls:AccessorChainEndingWithProperty> "." <func:FunctionCall> => {
ls.push(AccessorType::FunctionCall(func));
ls
},
}
AccessorChainEndingWithProperty: Vec<AccessorType> = {
Identifier => vec![AccessorType::Identifier(<>)],
<func:FunctionCall> "." <id:Identifier> => vec![
AccessorType::FunctionCall(func),
AccessorType::Identifier(id),
],
<mut first:AccessorChainEndingWithProperty> "." <id:Identifier> => {
first.push(AccessorType::Identifier(id));
first
},
<mut first:AccessorChainEndingWithProperty> "." <func:FunctionCall> "." <id:Identifier> => {
first.push(AccessorType::FunctionCall(func));
first.push(AccessorType::Identifier(id));
first
},
}
Conditional: ValueExpression = {
GuardedBlockList => Conditional { blocks: <>, fallback: None }.into(),
<ls:GuardedBlockList> "else" <fb:ExpressionBlock> => Conditional { blocks: ls, fallback: Some(fb) }.into(),
}
GuardedBlockList: Vec<GuardedBlock> = {
GuardedBlock => vec![<>],
<mut ls:GuardedBlockList> "else" <gb:GuardedBlock> => {
ls.push(gb);
ls
},
}
GuardedBlock: GuardedBlock = {
"if" <guard:ValueExpression> <block:ExpressionBlock> => GuardedBlock {
guard: Box::new(guard),
block,
},
}
FunctionCall: FunctionCall = {
<id:Identifier> <params:ParameterList> => FunctionCall {
name: id,
params,
}
IdentifierAsAlias: IdentifierAlias = {
<base:Identifier> "as" <alias:Identifier> => IdentifierAlias(base.0, alias.0),
};
Identifier: Identifier = {
"identifier" => Identifier(String::from(<>))
};
Literal: LiteralNode = {
"boolean" => LiteralNode::Boolean(<>),
"null" => LiteralNode::Null,
"float" => LiteralNode::Number(Number::Float(<>)),
"integer" => LiteralNode::Number(Number::Integer(<>)),
"string" => LiteralNode::String(String::from(<>)),
"owned_string" => LiteralNode::String(<>),
StringValue: String = {
"string" => String::from(<>),
"owned_string" => <>,

Louis
committed
CommaSeperatedList<Type>: Vec<Type> = {
Type => vec![<>],
<mut ls:CommaSeperatedList<Type>> "," <ty:Type> => {
ls.push(ty);
ls
}
}
"(" => ScriptTokenType::LeftParen,
")" => ScriptTokenType::RightParen,
"{" => ScriptTokenType::LeftBrace,
"}" => ScriptTokenType::RightBrace,
"," => ScriptTokenType::Comma,
"." => ScriptTokenType::Dot,
"-" => ScriptTokenType::Minus,
"+" => ScriptTokenType::Plus,
";" => ScriptTokenType::Semicolon,

Louis
committed
":" => ScriptTokenType::Colon,
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"/" => ScriptTokenType::Slash,
"*" => ScriptTokenType::Asterisk,
"!" => ScriptTokenType::Bang,
"!=" => ScriptTokenType::BangEqual,
"=" => ScriptTokenType::Equal,
"==" => ScriptTokenType::EqualEqual,
">" => ScriptTokenType::Greater,
">=" => ScriptTokenType::GreaterEqual,
"<" => ScriptTokenType::Less,
"<=" => ScriptTokenType::LessEqual,
"||" => ScriptTokenType::DoublePipe,
"&&" => ScriptTokenType::DoubleAmpersand,
"%" => ScriptTokenType::Modulo,
"^" => ScriptTokenType::Caret,
"struct" => ScriptTokenType::Class,
"else" => ScriptTokenType::Else,
"fn" => ScriptTokenType::Function,
"for" => ScriptTokenType::For,
"if" => ScriptTokenType::If,
"null" => ScriptTokenType::Null,
"print" => ScriptTokenType::Print,
"return" => ScriptTokenType::Return,
"super" => ScriptTokenType::Super,
"this" => ScriptTokenType::This,
"let" => ScriptTokenType::Let,
"while" => ScriptTokenType::While,
"export" => ScriptTokenType::Export,
"import" => ScriptTokenType::Import,
"as" => ScriptTokenType::Alias,
"from" => ScriptTokenType::From,
"typeof" => ScriptTokenType::Typeof,
"finally" => ScriptTokenType::Finally,
"boolean" => ScriptTokenType::Boolean(<bool>),
"float" => ScriptTokenType::Float(<f64>),
"integer" => ScriptTokenType::Integer(<i64>),
"owned_string" => ScriptTokenType::OwnedString(<String>),
"string" => ScriptTokenType::String(<String>),
"identifier" => ScriptTokenType::Identifier(<String>),