Skip to content

Commit

Permalink
Nitpick my Winnow code
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchalmers committed Oct 30, 2023
1 parent b271d50 commit 4634587
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
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((symbol, TokenType::Brace))
.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 (&str, TokenType) {
fn contains_token(&self, token: Token) -> bool {
self.0 == token.value && self.1 == token.token_type
}
}

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

0 comments on commit 4634587

Please sign in to comment.