145 lines
3.9 KiB
Plaintext
145 lines
3.9 KiB
Plaintext
|
|
%start Body
|
||
|
|
|
||
|
|
%left '||'
|
||
|
|
%left '&&'
|
||
|
|
%left '<' '>' '<=' '>=' '==' '!='
|
||
|
|
%left '*' '/'
|
||
|
|
%left '+' '-'
|
||
|
|
|
||
|
|
%%
|
||
|
|
|
||
|
|
Body -> Result<Vec<Stmt>>:
|
||
|
|
Body 'EOL' Stmt {
|
||
|
|
flatten($1, $3)
|
||
|
|
}
|
||
|
|
| Stmt { Ok(vec![$1?]) }
|
||
|
|
| { Ok(Vec::new()) }
|
||
|
|
;
|
||
|
|
|
||
|
|
Stmt -> Result<Stmt>:
|
||
|
|
Expr { Ok(Stmt::Expr($1?)) }
|
||
|
|
;
|
||
|
|
|
||
|
|
Expr -> Result<Expr>: BinExpr { $1 };
|
||
|
|
|
||
|
|
BinExpr -> Result<Expr>:
|
||
|
|
UnExpr '||' BinExpr { Ok(BinExpr::new_expr($1?, BinOp::Or, $3?)) }
|
||
|
|
| UnExpr '&&' BinExpr { Ok(BinExpr::new_expr($1?, BinOp::And, $3?)) }
|
||
|
|
| UnExpr '<' BinExpr { Ok(BinExpr::new_expr($1?, BinOp::Lt, $3?)) }
|
||
|
|
| UnExpr '>' BinExpr { Ok(BinExpr::new_expr($1?, BinOp::Gt, $3?)) }
|
||
|
|
| UnExpr '<=' BinExpr { Ok(BinExpr::new_expr($1?, BinOp::Le, $3?)) }
|
||
|
|
| UnExpr '>=' BinExpr { Ok(BinExpr::new_expr($1?, BinOp::Ge, $3?)) }
|
||
|
|
| UnExpr '==' BinExpr { Ok(BinExpr::new_expr($1?, BinOp::Eq, $3?)) }
|
||
|
|
| UnExpr '!=' BinExpr { Ok(BinExpr::new_expr($1?, BinOp::Neq, $3?)) }
|
||
|
|
| UnExpr '*' BinExpr { Ok(BinExpr::new_expr($1?, BinOp::Times, $3?)) }
|
||
|
|
| UnExpr '/' BinExpr { Ok(BinExpr::new_expr($1?, BinOp::Div, $3?)) }
|
||
|
|
| UnExpr '+' BinExpr { Ok(BinExpr::new_expr($1?, BinOp::Plus, $3?)) }
|
||
|
|
| UnExpr '-' BinExpr { Ok(BinExpr::new_expr($1?, BinOp::Plus, $3?)) }
|
||
|
|
| UnExpr { $1 }
|
||
|
|
;
|
||
|
|
|
||
|
|
UnExpr -> Result<Expr>:
|
||
|
|
'+' UnExpr { Ok(UnExpr::new_expr(UnOp::Plus, $2?)) }
|
||
|
|
| '-' UnExpr { Ok(UnExpr::new_expr(UnOp::Minus, $2?)) }
|
||
|
|
| AccessExpr { $1 }
|
||
|
|
;
|
||
|
|
|
||
|
|
AccessExpr -> Result<Expr>:
|
||
|
|
AtomExpr AccessExprTail {
|
||
|
|
Ok(AccessExpr::new_expr($1?, $2?))
|
||
|
|
}
|
||
|
|
;
|
||
|
|
|
||
|
|
AccessExprTail -> Result<Vec<Access>>:
|
||
|
|
AccessExprTail '.' Ident ExprTrailing {
|
||
|
|
flatten($1, Ok(Access { access: $3?, trailing: $4? }))
|
||
|
|
}
|
||
|
|
| '.' Ident ExprTrailing {
|
||
|
|
Ok(vec![Access { access: $2?, trailing: $3? }])
|
||
|
|
}
|
||
|
|
;
|
||
|
|
|
||
|
|
ExprTrailing -> Result<Vec<ExprTrail>>:
|
||
|
|
ExprTrailing '(' FunArgs ')' { flatten($1, Ok(ExprTrail::Call($3?))) }
|
||
|
|
| ExprTrailing '[' Expr ']' { flatten($1, Ok(ExprTrail::Index($3?))) }
|
||
|
|
| { Ok(Vec::new()) }
|
||
|
|
;
|
||
|
|
|
||
|
|
FunArgs -> Result<Vec<Expr>>:
|
||
|
|
FunArgsTail { $1 }
|
||
|
|
| { Ok(Vec::new()) }
|
||
|
|
;
|
||
|
|
|
||
|
|
FunArgsTail -> Result<Vec<Expr>>:
|
||
|
|
FunArgsTail ',' Expr { flatten($1, $3) }
|
||
|
|
| Expr { Ok(vec![$1?]) }
|
||
|
|
;
|
||
|
|
|
||
|
|
AtomExpr -> Result<Expr>:
|
||
|
|
Atom { $1.map(Expr::Atom) }
|
||
|
|
| '(' Expr ')' { $2 }
|
||
|
|
;
|
||
|
|
|
||
|
|
Atom -> Result<Atom>:
|
||
|
|
Ident { Ok(Atom::Ident($1?)) }
|
||
|
|
| 'SYM' {
|
||
|
|
let v = $1.map_err(|_| ())?;
|
||
|
|
let sym = &$lexer.span_str(v.span())[1..];
|
||
|
|
Ok(Atom::Sym(sym.to_string()))
|
||
|
|
}
|
||
|
|
| 'NUM' {
|
||
|
|
let v = $1.map_err(|_| ())?;
|
||
|
|
let num_text = &$lexer.span_str(v.span());
|
||
|
|
Ok(Atom::Num(num_text.parse().unwrap()))
|
||
|
|
}
|
||
|
|
| 'STRING' {
|
||
|
|
let v = $1.map_err(|_| ())?;
|
||
|
|
let string = &$lexer.span_str(v.span())[1..];
|
||
|
|
Ok(Atom::String(parse_string(string)))
|
||
|
|
}
|
||
|
|
;
|
||
|
|
|
||
|
|
Ident -> Result<String>:
|
||
|
|
'IDENT' {
|
||
|
|
let v = $1.map_err(|_| ())?;
|
||
|
|
let ident = $lexer.span_str(v.span()).to_string();
|
||
|
|
Ok(ident)
|
||
|
|
}
|
||
|
|
;
|
||
|
|
|
||
|
|
%%
|
||
|
|
|
||
|
|
use crate::syn::ast::*;
|
||
|
|
|
||
|
|
type Result<T> = std::result::Result<T, ()>;
|
||
|
|
|
||
|
|
fn flatten<T>(head: Result<Vec<T>>, tail: Result<T>) -> Result<Vec<T>> {
|
||
|
|
let mut head = head?;
|
||
|
|
let tail = tail?;
|
||
|
|
head.push(tail);
|
||
|
|
Ok(head)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn parse_string(input: &str) -> String {
|
||
|
|
let mut s = String::new();
|
||
|
|
let input = &input[1..input.bytes().len() - 1];
|
||
|
|
let mut chars = input.chars();
|
||
|
|
while let Some(c) = chars.next() {
|
||
|
|
if c == '\\' {
|
||
|
|
let next = chars.next().unwrap();
|
||
|
|
let c = match next {
|
||
|
|
'\\' => '\\',
|
||
|
|
'\'' => '\'',
|
||
|
|
'"' => '"',
|
||
|
|
'n' => '\n',
|
||
|
|
't' => '\t',
|
||
|
|
_ => unreachable!(),
|
||
|
|
};
|
||
|
|
s.push(c);
|
||
|
|
} else {
|
||
|
|
s.push(c);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
s
|
||
|
|
}
|