Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nitpick my Winnow code #946

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/wasm-lib/kcl/src/parser/parser_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use winnow::{
dispatch,
error::{ErrMode, StrContext, StrContextValue},
prelude::*,
token::any,
token::{any, one_of},
};

use crate::{
Expand Down Expand Up @@ -77,8 +77,7 @@ fn non_code_node(i: TokenSlice) -> PResult<NonCodeNode> {
/// Matches one case of NonCodeValue
/// See docstring on [NonCodeValue::NewLineBlockComment] for why that case is different to the others.
fn non_code_node_leading_whitespace(i: TokenSlice) -> PResult<NonCodeNode> {
let leading_whitespace = any
.verify(|token: &Token| token.token_type == TokenType::Whitespace)
let leading_whitespace = one_of(TokenType::Whitespace)
.context(expected("whitespace, with a newline"))
.parse_next(i)?;
let has_empty_line = count_in('\n', &leading_whitespace.value) >= 2;
Expand Down Expand Up @@ -378,7 +377,7 @@ fn whitespace(i: TokenSlice) -> PResult<Vec<Token>> {

/// Parse the = operator.
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, ="))
.parse_next(i)
}
Expand Down Expand Up @@ -1103,8 +1102,7 @@ fn expression(i: TokenSlice) -> PResult<ExpressionStatement> {

/// Parse a KCL integer, and the token that held it.
fn integer(i: TokenSlice) -> PResult<(Token, u64)> {
let num = any
.verify(|token: &Token| matches!(token.token_type, TokenType::Number))
let num = one_of(TokenType::Number)
.context(expected("a number token e.g. 3"))
.try_map(|token: Token| {
let source_ranges = token.as_source_ranges();
Expand All @@ -1123,28 +1121,29 @@ fn integer(i: TokenSlice) -> PResult<(Token, u64)> {

/// Parse the given brace symbol.
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))
.parse_next(i)
}

/// Parse a => operator.
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"))
.parse_next(i)
}
/// Parse a |> operator.
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(
"the |> operator, used for 'piping' one function's output into another function's input",
))
.parse_next(i)
}

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"))
.parse_next(i)
}
Expand Down
13 changes: 13 additions & 0 deletions src/wasm-lib/kcl/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use parse_display::{Display, FromStr};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use tower_lsp::lsp_types::SemanticTokenType;
use winnow::stream::ContainsToken;

use crate::{ast::types::VariableKind, executor::SourceRange};

Expand Down Expand Up @@ -129,6 +130,18 @@ pub struct Token {
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 {
pub fn from_range(range: std::ops::Range<usize>, token_type: TokenType, value: String) -> Self {
Self {
Expand Down
Loading