Partial fix #57

This commit is contained in:
Shunsuke Shibayama 2022-08-18 18:40:30 +09:00
parent 53f665355b
commit 3314825fc1
4 changed files with 38 additions and 29 deletions

View file

@ -952,8 +952,6 @@ impl CodeGenerator {
TokenKind::PrePlus => UNARY_POSITIVE,
TokenKind::PreMinus => UNARY_NEGATIVE,
TokenKind::Mutate => NOP, // ERG_MUTATE,
// TokenKind::PreStar =>,
// TokenKind::PreRng =>,
_ => {
self.errs.push(CompileError::feature_error(
self.cfg.input.clone(),

View file

@ -217,7 +217,8 @@ impl Lexer /*<'a>*/ {
| TokenCategory::Separator
| TokenCategory::SpecialBinOp
| TokenCategory::DefOp
| TokenCategory::LambdaOp => Some(false),
| TokenCategory::LambdaOp
| TokenCategory::BOF => Some(false),
// bin: `] +`, `1 +`, `true and[true]`
TokenCategory::REnclosure | TokenCategory::Literal => Some(true),
// bin: `fn +1`
@ -712,10 +713,13 @@ impl Iterator for Lexer /*<'a>*/ {
}
Some('?') => self.accept(Try, "?"),
Some('+') => {
let kind = if self.is_bin_position().unwrap() {
Plus
} else {
PrePlus
let kind = match self.is_bin_position() {
Some(true) => Plus,
Some(false) => PrePlus,
None => {
let token = self.emit_token(Illegal, "+");
return Some(Err(LexError::simple_syntax_error(0, token.loc())));
}
};
self.accept(kind, "+")
}
@ -725,18 +729,23 @@ impl Iterator for Lexer /*<'a>*/ {
self.accept(FuncArrow, "->")
}
_ => {
if self.is_bin_position().unwrap() {
self.accept(Minus, "-")
} else {
// IntLit (negative number)
if self
.peek_cur_ch()
.map(|t| t.is_ascii_digit())
.unwrap_or(false)
{
Some(self.lex_num('-'))
} else {
self.accept(Minus, "-")
match self.is_bin_position() {
Some(true) => self.accept(Minus, "-"),
Some(false) => {
// IntLit (negative number)
if self
.peek_cur_ch()
.map(|t| t.is_ascii_digit())
.unwrap_or(false)
{
Some(self.lex_num('-'))
} else {
self.accept(PreMinus, "-")
}
}
None => {
let token = self.emit_token(Illegal, "-");
return Some(Err(LexError::simple_syntax_error(0, token.loc())));
}
}
}
@ -747,10 +756,12 @@ impl Iterator for Lexer /*<'a>*/ {
self.accept(Pow, "**")
}
_ => {
let kind = if self.is_bin_position().unwrap() {
Star
} else {
PreStar
let kind = match self.is_bin_position() {
Some(true) => Star,
_ => {
let token = self.emit_token(Illegal, "*");
return Some(Err(LexError::simple_syntax_error(0, token.loc())));
}
};
self.accept(kind, "*")
}

View file

@ -836,7 +836,7 @@ impl Parser {
// TODO: range pattern
Ok(ParamPattern::Lit(self.try_reduce_lit()?))
}
Some(t) if t.is(PreStar) => {
Some(t) if t.is(Spread) => {
self.skip();
Ok(ParamPattern::VarArgsName(self.try_reduce_name()?))
}
@ -1420,7 +1420,7 @@ impl Parser {
let token = self.lpop();
Err(ParseError::feature_error(0, token.loc(), "discard pattern"))
}
_ => Err(self.skip_and_throw_syntax_err(caused_by!())),
_other => Err(self.skip_and_throw_syntax_err(caused_by!())),
}
}

View file

@ -32,8 +32,6 @@ pub enum TokenKind {
PrePlus,
/// `-` (unary)
PreMinus,
/// `*` (unary)
PreStar,
/// ~ (unary)
PreBitNot,
// PreAmp, // & (unary)
@ -229,6 +227,7 @@ pub enum TokenCategory {
VBar,
/// _
UBar,
BOF,
EOF,
Illegal,
}
@ -245,7 +244,7 @@ impl TokenKind {
Symbol => TokenCategory::Symbol,
NatLit | IntLit | RatioLit | StrLit | BoolLit | NoneLit | EllipsisLit | NoImplLit
| InfLit => TokenCategory::Literal,
PrePlus | PreMinus | PreStar | PreBitNot | Mutate => TokenCategory::UnaryOp,
PrePlus | PreMinus | PreBitNot | Mutate => TokenCategory::UnaryOp,
Try => TokenCategory::PostfixOp,
Comma | Colon | DblColon | SupertypeOf | SubtypeOf | Dot | Pipe | OrEqual => {
TokenCategory::SpecialBinOp
@ -260,8 +259,9 @@ impl TokenKind {
AtSign => TokenCategory::AtSign,
VBar => TokenCategory::VBar,
UBar => TokenCategory::UBar,
BOF => TokenCategory::BOF,
EOF => TokenCategory::EOF,
Illegal | BOF => TokenCategory::Illegal,
Illegal => TokenCategory::Illegal,
_ => TokenCategory::BinOp,
}
}