Nitpick my Winnow code (#946)
This commit is contained in:
@ -4,7 +4,7 @@ use winnow::{
|
|||||||
dispatch,
|
dispatch,
|
||||||
error::{ErrMode, StrContext, StrContextValue},
|
error::{ErrMode, StrContext, StrContextValue},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
token::any,
|
token::{any, one_of},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -77,8 +77,7 @@ fn non_code_node(i: TokenSlice) -> PResult<NonCodeNode> {
|
|||||||
/// Matches one case of NonCodeValue
|
/// Matches one case of NonCodeValue
|
||||||
/// See docstring on [NonCodeValue::NewLineBlockComment] for why that case is different to the others.
|
/// See docstring on [NonCodeValue::NewLineBlockComment] for why that case is different to the others.
|
||||||
fn non_code_node_leading_whitespace(i: TokenSlice) -> PResult<NonCodeNode> {
|
fn non_code_node_leading_whitespace(i: TokenSlice) -> PResult<NonCodeNode> {
|
||||||
let leading_whitespace = any
|
let leading_whitespace = one_of(TokenType::Whitespace)
|
||||||
.verify(|token: &Token| token.token_type == TokenType::Whitespace)
|
|
||||||
.context(expected("whitespace, with a newline"))
|
.context(expected("whitespace, with a newline"))
|
||||||
.parse_next(i)?;
|
.parse_next(i)?;
|
||||||
let has_empty_line = count_in('\n', &leading_whitespace.value) >= 2;
|
let has_empty_line = count_in('\n', &leading_whitespace.value) >= 2;
|
||||||
@ -378,7 +377,7 @@ fn whitespace(i: TokenSlice) -> PResult<Vec<Token>> {
|
|||||||
|
|
||||||
/// Parse the = operator.
|
/// Parse the = operator.
|
||||||
fn equals(i: TokenSlice) -> PResult<Token> {
|
fn equals(i: TokenSlice) -> PResult<Token> {
|
||||||
any.verify(|token: &Token| matches!(token.token_type, TokenType::Operator) && token.value == "=")
|
one_of((TokenType::Operator, "="))
|
||||||
.context(expected("the equals operator, ="))
|
.context(expected("the equals operator, ="))
|
||||||
.parse_next(i)
|
.parse_next(i)
|
||||||
}
|
}
|
||||||
@ -1103,8 +1102,7 @@ fn expression(i: TokenSlice) -> PResult<ExpressionStatement> {
|
|||||||
|
|
||||||
/// Parse a KCL integer, and the token that held it.
|
/// Parse a KCL integer, and the token that held it.
|
||||||
fn integer(i: TokenSlice) -> PResult<(Token, u64)> {
|
fn integer(i: TokenSlice) -> PResult<(Token, u64)> {
|
||||||
let num = any
|
let num = one_of(TokenType::Number)
|
||||||
.verify(|token: &Token| matches!(token.token_type, TokenType::Number))
|
|
||||||
.context(expected("a number token e.g. 3"))
|
.context(expected("a number token e.g. 3"))
|
||||||
.try_map(|token: Token| {
|
.try_map(|token: Token| {
|
||||||
let source_ranges = token.as_source_ranges();
|
let source_ranges = token.as_source_ranges();
|
||||||
@ -1123,20 +1121,20 @@ fn integer(i: TokenSlice) -> PResult<(Token, u64)> {
|
|||||||
|
|
||||||
/// Parse the given brace symbol.
|
/// Parse the given brace symbol.
|
||||||
fn some_brace(symbol: &'static str, i: TokenSlice) -> PResult<Token> {
|
fn some_brace(symbol: &'static str, i: TokenSlice) -> PResult<Token> {
|
||||||
any.verify(|token: &Token| matches!(token.token_type, TokenType::Brace) && token.value == symbol)
|
one_of((TokenType::Brace, symbol))
|
||||||
.context(expected(symbol))
|
.context(expected(symbol))
|
||||||
.parse_next(i)
|
.parse_next(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a => operator.
|
/// Parse a => operator.
|
||||||
fn big_arrow(i: TokenSlice) -> PResult<Token> {
|
fn big_arrow(i: TokenSlice) -> PResult<Token> {
|
||||||
any.verify(|token: &Token| matches!(token.token_type, TokenType::Operator) && token.value == "=>")
|
one_of((TokenType::Operator, "=>"))
|
||||||
.context(expected("the => symbol, used for declaring functions"))
|
.context(expected("the => symbol, used for declaring functions"))
|
||||||
.parse_next(i)
|
.parse_next(i)
|
||||||
}
|
}
|
||||||
/// Parse a |> operator.
|
/// Parse a |> operator.
|
||||||
fn pipe_operator(i: TokenSlice) -> PResult<Token> {
|
fn pipe_operator(i: TokenSlice) -> PResult<Token> {
|
||||||
any.verify(|token: &Token| matches!(token.token_type, TokenType::Operator) && token.value == "|>")
|
one_of((TokenType::Operator, "|>"))
|
||||||
.context(expected(
|
.context(expected(
|
||||||
"the |> operator, used for 'piping' one function's output into another function's input",
|
"the |> operator, used for 'piping' one function's output into another function's input",
|
||||||
))
|
))
|
||||||
@ -1144,7 +1142,8 @@ fn pipe_operator(i: TokenSlice) -> PResult<Token> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn ws_with_newline(i: TokenSlice) -> PResult<Token> {
|
fn ws_with_newline(i: TokenSlice) -> PResult<Token> {
|
||||||
any.verify(|token: &Token| matches!(token.token_type, TokenType::Whitespace) && token.value.contains('\n'))
|
one_of(TokenType::Whitespace)
|
||||||
|
.verify(|token: &Token| token.value.contains('\n'))
|
||||||
.context(expected("a newline, possibly with whitespace"))
|
.context(expected("a newline, possibly with whitespace"))
|
||||||
.parse_next(i)
|
.parse_next(i)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,7 @@ use parse_display::{Display, FromStr};
|
|||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tower_lsp::lsp_types::SemanticTokenType;
|
use tower_lsp::lsp_types::SemanticTokenType;
|
||||||
|
use winnow::stream::ContainsToken;
|
||||||
|
|
||||||
use crate::{ast::types::VariableKind, executor::SourceRange};
|
use crate::{ast::types::VariableKind, executor::SourceRange};
|
||||||
|
|
||||||
@ -129,6 +130,18 @@ pub struct Token {
|
|||||||
pub value: String,
|
pub value: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ContainsToken<Token> for (TokenType, &str) {
|
||||||
|
fn contains_token(&self, token: Token) -> bool {
|
||||||
|
self.0 == token.token_type && self.1 == token.value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ContainsToken<Token> for TokenType {
|
||||||
|
fn contains_token(&self, token: Token) -> bool {
|
||||||
|
*self == token.token_type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Token {
|
impl Token {
|
||||||
pub fn from_range(range: std::ops::Range<usize>, token_type: TokenType, value: String) -> Self {
|
pub fn from_range(range: std::ops::Range<usize>, token_type: TokenType, value: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|||||||
Reference in New Issue
Block a user