mirror of https://github.com/astral-sh/ruff
refactor: `StateMachine` use `match` statement (#3811)
This commit is contained in:
parent
a142d71e0b
commit
d7113d3995
|
|
@ -32,86 +32,91 @@ pub struct StateMachine {
|
||||||
|
|
||||||
impl StateMachine {
|
impl StateMachine {
|
||||||
pub fn consume(&mut self, tok: &Tok) -> bool {
|
pub fn consume(&mut self, tok: &Tok) -> bool {
|
||||||
if matches!(
|
match tok {
|
||||||
tok,
|
Tok::NonLogicalNewline
|
||||||
Tok::NonLogicalNewline | Tok::Newline | Tok::Indent | Tok::Dedent | Tok::Comment(..)
|
| Tok::Newline
|
||||||
) {
|
| Tok::Indent
|
||||||
return false;
|
| Tok::Dedent
|
||||||
}
|
| Tok::Comment(..) => false,
|
||||||
|
|
||||||
if matches!(tok, Tok::String { .. }) {
|
Tok::String { .. } => {
|
||||||
return if matches!(
|
if matches!(
|
||||||
self.state,
|
self.state,
|
||||||
State::ExpectModuleDocstring
|
State::ExpectModuleDocstring
|
||||||
| State::ExpectClassDocstring
|
| State::ExpectClassDocstring
|
||||||
| State::ExpectFunctionDocstring
|
| State::ExpectFunctionDocstring
|
||||||
) {
|
) {
|
||||||
self.state = State::Other;
|
self.state = State::Other;
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches!(tok, Tok::Class) {
|
|
||||||
self.state = State::ExpectClassColon;
|
|
||||||
self.bracket_count = 0;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches!(tok, Tok::Def) {
|
|
||||||
self.state = State::ExpectFunctionColon;
|
|
||||||
self.bracket_count = 0;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches!(tok, Tok::Colon) {
|
|
||||||
if self.bracket_count == 0 {
|
|
||||||
if matches!(self.state, State::ExpectClassColon) {
|
|
||||||
self.state = State::ExpectClassDocstring;
|
|
||||||
} else if matches!(self.state, State::ExpectFunctionColon) {
|
|
||||||
self.state = State::ExpectFunctionDocstring;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
Tok::Class => {
|
||||||
}
|
self.state = State::ExpectClassColon;
|
||||||
|
self.bracket_count = 0;
|
||||||
|
|
||||||
if matches!(tok, Tok::Lpar | Tok::Lbrace | Tok::Lsqb) {
|
false
|
||||||
self.bracket_count += 1;
|
|
||||||
if matches!(
|
|
||||||
self.state,
|
|
||||||
State::ExpectModuleDocstring
|
|
||||||
| State::ExpectClassDocstring
|
|
||||||
| State::ExpectFunctionDocstring
|
|
||||||
) {
|
|
||||||
self.state = State::Other;
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches!(tok, Tok::Rpar | Tok::Rbrace | Tok::Rsqb) {
|
Tok::Def => {
|
||||||
self.bracket_count -= 1;
|
self.state = State::ExpectFunctionColon;
|
||||||
if matches!(
|
self.bracket_count = 0;
|
||||||
self.state,
|
|
||||||
State::ExpectModuleDocstring
|
false
|
||||||
| State::ExpectClassDocstring
|
|
||||||
| State::ExpectFunctionDocstring
|
|
||||||
) {
|
|
||||||
self.state = State::Other;
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if matches!(
|
Tok::Colon => {
|
||||||
self.state,
|
if self.bracket_count == 0 {
|
||||||
State::ExpectModuleDocstring
|
if matches!(self.state, State::ExpectClassColon) {
|
||||||
| State::ExpectClassDocstring
|
self.state = State::ExpectClassDocstring;
|
||||||
| State::ExpectFunctionDocstring
|
} else if matches!(self.state, State::ExpectFunctionColon) {
|
||||||
) {
|
self.state = State::ExpectFunctionDocstring;
|
||||||
self.state = State::Other;
|
}
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
false
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
Tok::Lpar | Tok::Lbrace | Tok::Lsqb => {
|
||||||
|
self.bracket_count += 1;
|
||||||
|
if matches!(
|
||||||
|
self.state,
|
||||||
|
State::ExpectModuleDocstring
|
||||||
|
| State::ExpectClassDocstring
|
||||||
|
| State::ExpectFunctionDocstring
|
||||||
|
) {
|
||||||
|
self.state = State::Other;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
Tok::Rpar | Tok::Rbrace | Tok::Rsqb => {
|
||||||
|
self.bracket_count -= 1;
|
||||||
|
if matches!(
|
||||||
|
self.state,
|
||||||
|
State::ExpectModuleDocstring
|
||||||
|
| State::ExpectClassDocstring
|
||||||
|
| State::ExpectFunctionDocstring
|
||||||
|
) {
|
||||||
|
self.state = State::Other;
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => {
|
||||||
|
if matches!(
|
||||||
|
self.state,
|
||||||
|
State::ExpectModuleDocstring
|
||||||
|
| State::ExpectClassDocstring
|
||||||
|
| State::ExpectFunctionDocstring
|
||||||
|
) {
|
||||||
|
self.state = State::Other;
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue