forked from Mirrors/helix
Merge branch 'master' of https://github.com/helix-editor/helix into tree_explore
commit
d043ea4db4
@ -0,0 +1,525 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use helix_core::{smallvec, SmallVec};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum CaseChange {
|
||||
Upcase,
|
||||
Downcase,
|
||||
Capitalize,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum FormatItem<'a> {
|
||||
Text(&'a str),
|
||||
Capture(usize),
|
||||
CaseChange(usize, CaseChange),
|
||||
Conditional(usize, Option<&'a str>, Option<&'a str>),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Regex<'a> {
|
||||
value: &'a str,
|
||||
replacement: Vec<FormatItem<'a>>,
|
||||
options: Option<&'a str>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum SnippetElement<'a> {
|
||||
Tabstop {
|
||||
tabstop: usize,
|
||||
},
|
||||
Placeholder {
|
||||
tabstop: usize,
|
||||
value: Vec<SnippetElement<'a>>,
|
||||
},
|
||||
Choice {
|
||||
tabstop: usize,
|
||||
choices: Vec<&'a str>,
|
||||
},
|
||||
Variable {
|
||||
name: &'a str,
|
||||
default: Option<&'a str>,
|
||||
regex: Option<Regex<'a>>,
|
||||
},
|
||||
Text(&'a str),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct Snippet<'a> {
|
||||
elements: Vec<SnippetElement<'a>>,
|
||||
}
|
||||
|
||||
pub fn parse(s: &str) -> Result<Snippet<'_>> {
|
||||
parser::parse(s).map_err(|rest| anyhow!("Failed to parse snippet. Remaining input: {}", rest))
|
||||
}
|
||||
|
||||
fn render_elements(
|
||||
snippet_elements: &[SnippetElement<'_>],
|
||||
insert: &mut String,
|
||||
offset: &mut usize,
|
||||
tabstops: &mut Vec<(usize, (usize, usize))>,
|
||||
newline_with_offset: &String,
|
||||
include_placeholer: bool,
|
||||
) {
|
||||
use SnippetElement::*;
|
||||
|
||||
for element in snippet_elements {
|
||||
match element {
|
||||
&Text(text) => {
|
||||
// small optimization to avoid calling replace when it's unnecessary
|
||||
let text = if text.contains('\n') {
|
||||
Cow::Owned(text.replace('\n', newline_with_offset))
|
||||
} else {
|
||||
Cow::Borrowed(text)
|
||||
};
|
||||
*offset += text.chars().count();
|
||||
insert.push_str(&text);
|
||||
}
|
||||
&Variable {
|
||||
name: _,
|
||||
regex: _,
|
||||
r#default,
|
||||
} => {
|
||||
// TODO: variables. For now, fall back to the default, which defaults to "".
|
||||
let text = r#default.unwrap_or_default();
|
||||
*offset += text.chars().count();
|
||||
insert.push_str(text);
|
||||
}
|
||||
&Tabstop { tabstop } => {
|
||||
tabstops.push((tabstop, (*offset, *offset)));
|
||||
}
|
||||
Placeholder {
|
||||
tabstop,
|
||||
value: inner_snippet_elements,
|
||||
} => {
|
||||
let start_offset = *offset;
|
||||
if include_placeholer {
|
||||
render_elements(
|
||||
inner_snippet_elements,
|
||||
insert,
|
||||
offset,
|
||||
tabstops,
|
||||
newline_with_offset,
|
||||
include_placeholer,
|
||||
);
|
||||
}
|
||||
tabstops.push((*tabstop, (start_offset, *offset)));
|
||||
}
|
||||
&Choice {
|
||||
tabstop,
|
||||
choices: _,
|
||||
} => {
|
||||
// TODO: choices
|
||||
tabstops.push((tabstop, (*offset, *offset)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::type_complexity)] // only used one time
|
||||
pub fn render(
|
||||
snippet: &Snippet<'_>,
|
||||
newline_with_offset: String,
|
||||
include_placeholer: bool,
|
||||
) -> (String, Vec<SmallVec<[(usize, usize); 1]>>) {
|
||||
let mut insert = String::new();
|
||||
let mut tabstops = Vec::new();
|
||||
let mut offset = 0;
|
||||
|
||||
render_elements(
|
||||
&snippet.elements,
|
||||
&mut insert,
|
||||
&mut offset,
|
||||
&mut tabstops,
|
||||
&newline_with_offset,
|
||||
include_placeholer,
|
||||
);
|
||||
|
||||
// sort in ascending order (except for 0, which should always be the last one (per lsp doc))
|
||||
tabstops.sort_unstable_by_key(|(n, _)| if *n == 0 { usize::MAX } else { *n });
|
||||
|
||||
// merge tabstops with the same index (we take advantage of the fact that we just sorted them
|
||||
// above to simply look backwards)
|
||||
let mut ntabstops = Vec::<SmallVec<[(usize, usize); 1]>>::new();
|
||||
{
|
||||
let mut prev = None;
|
||||
for (tabstop, r) in tabstops {
|
||||
if prev == Some(tabstop) {
|
||||
let len_1 = ntabstops.len() - 1;
|
||||
ntabstops[len_1].push(r);
|
||||
} else {
|
||||
prev = Some(tabstop);
|
||||
ntabstops.push(smallvec![r]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(insert, ntabstops)
|
||||
}
|
||||
|
||||
mod parser {
|
||||
use helix_parsec::*;
|
||||
|
||||
use super::{CaseChange, FormatItem, Regex, Snippet, SnippetElement};
|
||||
|
||||
/*
|
||||
https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#snippet_syntax
|
||||
|
||||
any ::= tabstop | placeholder | choice | variable | text
|
||||
tabstop ::= '$' int | '${' int '}'
|
||||
placeholder ::= '${' int ':' any '}'
|
||||
choice ::= '${' int '|' text (',' text)* '|}'
|
||||
variable ::= '$' var | '${' var }'
|
||||
| '${' var ':' any '}'
|
||||
| '${' var '/' regex '/' (format | text)+ '/' options '}'
|
||||
format ::= '$' int | '${' int '}'
|
||||
| '${' int ':' '/upcase' | '/downcase' | '/capitalize' '}'
|
||||
| '${' int ':+' if '}'
|
||||
| '${' int ':?' if ':' else '}'
|
||||
| '${' int ':-' else '}' | '${' int ':' else '}'
|
||||
regex ::= Regular Expression value (ctor-string)
|
||||
options ::= Regular Expression option (ctor-options)
|
||||
var ::= [_a-zA-Z] [_a-zA-Z0-9]*
|
||||
int ::= [0-9]+
|
||||
text ::= .*
|
||||
if ::= text
|
||||
else ::= text
|
||||
*/
|
||||
|
||||
fn var<'a>() -> impl Parser<'a, Output = &'a str> {
|
||||
// var = [_a-zA-Z][_a-zA-Z0-9]*
|
||||
move |input: &'a str| match input
|
||||
.char_indices()
|
||||
.take_while(|(p, c)| {
|
||||
*c == '_'
|
||||
|| if *p == 0 {
|
||||
c.is_ascii_alphabetic()
|
||||
} else {
|
||||
c.is_ascii_alphanumeric()
|
||||
}
|
||||
})
|
||||
.last()
|
||||
{
|
||||
Some((index, c)) if index >= 1 => {
|
||||
let index = index + c.len_utf8();
|
||||
Ok((&input[index..], &input[0..index]))
|
||||
}
|
||||
_ => Err(input),
|
||||
}
|
||||
}
|
||||
|
||||
fn text<'a, const SIZE: usize>(cs: [char; SIZE]) -> impl Parser<'a, Output = &'a str> {
|
||||
take_while(move |c| cs.into_iter().all(|c1| c != c1))
|
||||
}
|
||||
|
||||
fn digit<'a>() -> impl Parser<'a, Output = usize> {
|
||||
filter_map(take_while(|c| c.is_ascii_digit()), |s| s.parse().ok())
|
||||
}
|
||||
|
||||
fn case_change<'a>() -> impl Parser<'a, Output = CaseChange> {
|
||||
use CaseChange::*;
|
||||
|
||||
choice!(
|
||||
map("upcase", |_| Upcase),
|
||||
map("downcase", |_| Downcase),
|
||||
map("capitalize", |_| Capitalize),
|
||||
)
|
||||
}
|
||||
|
||||
fn format<'a>() -> impl Parser<'a, Output = FormatItem<'a>> {
|
||||
use FormatItem::*;
|
||||
|
||||
choice!(
|
||||
// '$' int
|
||||
map(right("$", digit()), Capture),
|
||||
// '${' int '}'
|
||||
map(seq!("${", digit(), "}"), |seq| Capture(seq.1)),
|
||||
// '${' int ':' '/upcase' | '/downcase' | '/capitalize' '}'
|
||||
map(seq!("${", digit(), ":/", case_change(), "}"), |seq| {
|
||||
CaseChange(seq.1, seq.3)
|
||||
}),
|
||||
// '${' int ':+' if '}'
|
||||
map(
|
||||
seq!("${", digit(), ":+", take_until(|c| c == '}'), "}"),
|
||||
|seq| { Conditional(seq.1, Some(seq.3), None) }
|
||||
),
|
||||
// '${' int ':?' if ':' else '}'
|
||||
map(
|
||||
seq!(
|
||||
"${",
|
||||
digit(),
|
||||
":?",
|
||||
take_until(|c| c == ':'),
|
||||
":",
|
||||
take_until(|c| c == '}'),
|
||||
"}"
|
||||
),
|
||||
|seq| { Conditional(seq.1, Some(seq.3), Some(seq.5)) }
|
||||
),
|
||||
// '${' int ':-' else '}' | '${' int ':' else '}'
|
||||
map(
|
||||
seq!(
|
||||
"${",
|
||||
digit(),
|
||||
":",
|
||||
optional("-"),
|
||||
take_until(|c| c == '}'),
|
||||
"}"
|
||||
),
|
||||
|seq| { Conditional(seq.1, None, Some(seq.4)) }
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fn regex<'a>() -> impl Parser<'a, Output = Regex<'a>> {
|
||||
let text = map(text(['$', '/']), FormatItem::Text);
|
||||
let replacement = reparse_as(
|
||||
take_until(|c| c == '/'),
|
||||
one_or_more(choice!(format(), text)),
|
||||
);
|
||||
|
||||
map(
|
||||
seq!(
|
||||
"/",
|
||||
take_until(|c| c == '/'),
|
||||
"/",
|
||||
replacement,
|
||||
"/",
|
||||
optional(take_until(|c| c == '}')),
|
||||
),
|
||||
|(_, value, _, replacement, _, options)| Regex {
|
||||
value,
|
||||
replacement,
|
||||
options,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn tabstop<'a>() -> impl Parser<'a, Output = SnippetElement<'a>> {
|
||||
map(
|
||||
or(
|
||||
right("$", digit()),
|
||||
map(seq!("${", digit(), "}"), |values| values.1),
|
||||
),
|
||||
|digit| SnippetElement::Tabstop { tabstop: digit },
|
||||
)
|
||||
}
|
||||
|
||||
fn placeholder<'a>() -> impl Parser<'a, Output = SnippetElement<'a>> {
|
||||
let text = map(text(['$', '}']), SnippetElement::Text);
|
||||
map(
|
||||
seq!(
|
||||
"${",
|
||||
digit(),
|
||||
":",
|
||||
one_or_more(choice!(anything(), text)),
|
||||
"}"
|
||||
),
|
||||
|seq| SnippetElement::Placeholder {
|
||||
tabstop: seq.1,
|
||||
value: seq.3,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn choice<'a>() -> impl Parser<'a, Output = SnippetElement<'a>> {
|
||||
map(
|
||||
seq!(
|
||||
"${",
|
||||
digit(),
|
||||
"|",
|
||||
sep(take_until(|c| c == ',' || c == '|'), ","),
|
||||
"|}",
|
||||
),
|
||||
|seq| SnippetElement::Choice {
|
||||
tabstop: seq.1,
|
||||
choices: seq.3,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn variable<'a>() -> impl Parser<'a, Output = SnippetElement<'a>> {
|
||||
choice!(
|
||||
// $var
|
||||
map(right("$", var()), |name| SnippetElement::Variable {
|
||||
name,
|
||||
default: None,
|
||||
regex: None,
|
||||
}),
|
||||
// ${var:default}
|
||||
map(
|
||||
seq!("${", var(), ":", take_until(|c| c == '}'), "}",),
|
||||
|values| SnippetElement::Variable {
|
||||
name: values.1,
|
||||
default: Some(values.3),
|
||||
regex: None,
|
||||
}
|
||||
),
|
||||
// ${var/value/format/options}
|
||||
map(seq!("${", var(), regex(), "}"), |values| {
|
||||
SnippetElement::Variable {
|
||||
name: values.1,
|
||||
default: None,
|
||||
regex: Some(values.2),
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
fn anything<'a>() -> impl Parser<'a, Output = SnippetElement<'a>> {
|
||||
// The parser has to be constructed lazily to avoid infinite opaque type recursion
|
||||
|input: &'a str| {
|
||||
let parser = choice!(tabstop(), placeholder(), choice(), variable());
|
||||
parser.parse(input)
|
||||
}
|
||||
}
|
||||
|
||||
fn snippet<'a>() -> impl Parser<'a, Output = Snippet<'a>> {
|
||||
let text = map(text(['$']), SnippetElement::Text);
|
||||
map(one_or_more(choice!(anything(), text)), |parts| Snippet {
|
||||
elements: parts,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse(s: &str) -> Result<Snippet, &str> {
|
||||
snippet().parse(s).map(|(_input, elements)| elements)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::SnippetElement::*;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn empty_string_is_error() {
|
||||
assert_eq!(Err(""), parse(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_placeholders_in_function_call() {
|
||||
assert_eq!(
|
||||
Ok(Snippet {
|
||||
elements: vec![
|
||||
Text("match("),
|
||||
Placeholder {
|
||||
tabstop: 1,
|
||||
value: vec!(Text("Arg1")),
|
||||
},
|
||||
Text(")")
|
||||
]
|
||||
}),
|
||||
parse("match(${1:Arg1})")
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_placeholders_in_statement() {
|
||||
assert_eq!(
|
||||
Ok(Snippet {
|
||||
elements: vec![
|
||||
Text("local "),
|
||||
Placeholder {
|
||||
tabstop: 1,
|
||||
value: vec!(Text("var")),
|
||||
},
|
||||
Text(" = "),
|
||||
Placeholder {
|
||||
tabstop: 1,
|
||||
value: vec!(Text("value")),
|
||||
},
|
||||
]
|
||||
}),
|
||||
parse("local ${1:var} = ${1:value}")
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_tabstop_nested_in_placeholder() {
|
||||
assert_eq!(
|
||||
Ok(Snippet {
|
||||
elements: vec![Placeholder {
|
||||
tabstop: 1,
|
||||
value: vec!(Text("var, "), Tabstop { tabstop: 2 },),
|
||||
},]
|
||||
}),
|
||||
parse("${1:var, $2}")
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_placeholder_nested_in_placeholder() {
|
||||
assert_eq!(
|
||||
Ok(Snippet {
|
||||
elements: vec![Placeholder {
|
||||
tabstop: 1,
|
||||
value: vec!(
|
||||
Text("foo "),
|
||||
Placeholder {
|
||||
tabstop: 2,
|
||||
value: vec!(Text("bar")),
|
||||
},
|
||||
),
|
||||
},]
|
||||
}),
|
||||
parse("${1:foo ${2:bar}}")
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_all() {
|
||||
assert_eq!(
|
||||
Ok(Snippet {
|
||||
elements: vec![
|
||||
Text("hello "),
|
||||
Tabstop { tabstop: 1 },
|
||||
Tabstop { tabstop: 2 },
|
||||
Text(" "),
|
||||
Choice {
|
||||
tabstop: 1,
|
||||
choices: vec!["one", "two", "three"]
|
||||
},
|
||||
Text(" "),
|
||||
Variable {
|
||||
name: "name",
|
||||
default: Some("foo"),
|
||||
regex: None
|
||||
},
|
||||
Text(" "),
|
||||
Variable {
|
||||
name: "var",
|
||||
default: None,
|
||||
regex: None
|
||||
},
|
||||
Text(" "),
|
||||
Variable {
|
||||
name: "TM",
|
||||
default: None,
|
||||
regex: None
|
||||
},
|
||||
]
|
||||
}),
|
||||
parse("hello $1${2} ${1|one,two,three|} ${name:foo} $var $TM")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn regex_capture_replace() {
|
||||
assert_eq!(
|
||||
Ok(Snippet {
|
||||
elements: vec![Variable {
|
||||
name: "TM_FILENAME",
|
||||
default: None,
|
||||
regex: Some(Regex {
|
||||
value: "(.*).+$",
|
||||
replacement: vec![FormatItem::Capture(1)],
|
||||
options: None,
|
||||
}),
|
||||
}]
|
||||
}),
|
||||
parse("${TM_FILENAME/(.*).+$/$1/}")
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "helix-parsec"
|
||||
version = "0.6.0"
|
||||
authors = ["Blaž Hrastnik <blaz@mxxn.io>"]
|
||||
edition = "2021"
|
||||
license = "MPL-2.0"
|
||||
description = "Parser combinators for Helix"
|
||||
categories = ["editor"]
|
||||
repository = "https://github.com/helix-editor/helix"
|
||||
homepage = "https://helix-editor.com"
|
||||
include = ["src/**/*", "README.md"]
|
||||
|
||||
[dependencies]
|
@ -0,0 +1,561 @@
|
||||
//! Parser-combinator functions
|
||||
//!
|
||||
//! This module provides parsers and parser combinators which can be used
|
||||
//! together to build parsers by functional composition.
|
||||
|
||||
// This module implements parser combinators following https://bodil.lol/parser-combinators/.
|
||||
// `sym` (trait implementation for `&'static str`), `map`, `pred` (filter), `one_or_more`,
|
||||
// `zero_or_more`, as well as the `Parser` trait originate mostly from that post.
|
||||
// The remaining parsers and parser combinators are either based on
|
||||
// https://github.com/archseer/snippets.nvim/blob/a583da6ef130d2a4888510afd8c4e5ffd62d0dce/lua/snippet/parser.lua#L5-L138
|
||||
// or are novel.
|
||||
|
||||
// When a parser matches the input successfully, it returns `Ok((next_input, some_value))`
|
||||
// where the type of the returned value depends on the parser. If the parser fails to match,
|
||||
// it returns `Err(input)`.
|
||||
type ParseResult<'a, Output> = Result<(&'a str, Output), &'a str>;
|
||||
|
||||
/// A parser or parser-combinator.
|
||||
///
|
||||
/// Parser-combinators compose multiple parsers together to parse input.
|
||||
/// For example, two basic parsers (`&'static str`s) may be combined with
|
||||
/// a parser-combinator like [or] to produce a new parser.
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{or, Parser};
|
||||
/// let foo = "foo"; // matches "foo" literally
|
||||
/// let bar = "bar"; // matches "bar" literally
|
||||
/// let foo_or_bar = or(foo, bar); // matches either "foo" or "bar"
|
||||
/// assert_eq!(Ok(("", "foo")), foo_or_bar.parse("foo"));
|
||||
/// assert_eq!(Ok(("", "bar")), foo_or_bar.parse("bar"));
|
||||
/// assert_eq!(Err("baz"), foo_or_bar.parse("baz"));
|
||||
/// ```
|
||||
pub trait Parser<'a> {
|
||||
type Output;
|
||||
|
||||
fn parse(&self, input: &'a str) -> ParseResult<'a, Self::Output>;
|
||||
}
|
||||
|
||||
// Most parser-combinators are written as higher-order functions which take some
|
||||
// parser(s) as input and return a new parser: a function that takes input and returns
|
||||
// a parse result. The underlying implementation of [Parser::parse] for these functions
|
||||
// is simply application.
|
||||
#[doc(hidden)]
|
||||
impl<'a, F, T> Parser<'a> for F
|
||||
where
|
||||
F: Fn(&'a str) -> ParseResult<T>,
|
||||
{
|
||||
type Output = T;
|
||||
|
||||
fn parse(&self, input: &'a str) -> ParseResult<'a, Self::Output> {
|
||||
self(input)
|
||||
}
|
||||
}
|
||||
|
||||
/// A parser which matches the string literal exactly.
|
||||
///
|
||||
/// This parser succeeds if the next characters in the input are equal to the given
|
||||
/// string literal.
|
||||
///
|
||||
/// Note that [str::parse] interferes with calling [Parser::parse] on string literals
|
||||
/// directly; this trait implementation works when used within any parser combinator
|
||||
/// but does not work on its own. To call [Parser::parse] on a parser for a string
|
||||
/// literal, use the [token] parser.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{or, Parser};
|
||||
/// let parser = or("foo", "bar");
|
||||
/// assert_eq!(Ok(("", "foo")), parser.parse("foo"));
|
||||
/// assert_eq!(Ok(("", "bar")), parser.parse("bar"));
|
||||
/// assert_eq!(Err("baz"), parser.parse("baz"));
|
||||
/// ```
|
||||
impl<'a> Parser<'a> for &'static str {
|
||||
type Output = &'a str;
|
||||
|
||||
fn parse(&self, input: &'a str) -> ParseResult<'a, Self::Output> {
|
||||
match input.get(0..self.len()) {
|
||||
Some(actual) if actual == *self => Ok((&input[self.len()..], &input[0..self.len()])),
|
||||
_ => Err(input),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parsers
|
||||
|
||||
/// A parser which matches the given string literally.
|
||||
///
|
||||
/// This function is a convenience for interpreting string literals as parsers
|
||||
/// and is only necessary to avoid conflict with [str::parse]. See the documentation
|
||||
/// for the `&'static str` implementation of [Parser].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{token, Parser};
|
||||
/// let parser = token("foo");
|
||||
/// assert_eq!(Ok(("", "foo")), parser.parse("foo"));
|
||||
/// assert_eq!(Err("bar"), parser.parse("bar"));
|
||||
/// ```
|
||||
pub fn token<'a>(literal: &'static str) -> impl Parser<'a, Output = &'a str> {
|
||||
literal
|
||||
}
|
||||
|
||||
/// A parser which matches all values until the specified pattern is found.
|
||||
///
|
||||
/// If the pattern is not found, this parser does not match. The input up to the
|
||||
/// character which returns `true` is returned but not that character itself.
|
||||
///
|
||||
/// If the pattern function returns true on the first input character, this
|
||||
/// parser fails.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{take_until, Parser};
|
||||
/// let parser = take_until(|c| c == '.');
|
||||
/// assert_eq!(Ok((".bar", "foo")), parser.parse("foo.bar"));
|
||||
/// assert_eq!(Err(".foo"), parser.parse(".foo"));
|
||||
/// assert_eq!(Err("foo"), parser.parse("foo"));
|
||||
/// ```
|
||||
pub fn take_until<'a, F>(pattern: F) -> impl Parser<'a, Output = &'a str>
|
||||
where
|
||||
F: Fn(char) -> bool,
|
||||
{
|
||||
move |input: &'a str| match input.find(&pattern) {
|
||||
Some(index) if index != 0 => Ok((&input[index..], &input[0..index])),
|
||||
_ => Err(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// A parser which matches all values until the specified pattern no longer match.
|
||||
///
|
||||
/// This parser only ever fails if the input has a length of zero.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{take_while, Parser};
|
||||
/// let parser = take_while(|c| c == '1');
|
||||
/// assert_eq!(Ok(("2", "11")), parser.parse("112"));
|
||||
/// assert_eq!(Err("22"), parser.parse("22"));
|
||||
/// ```
|
||||
pub fn take_while<'a, F>(pattern: F) -> impl Parser<'a, Output = &'a str>
|
||||
where
|
||||
F: Fn(char) -> bool,
|
||||
{
|
||||
move |input: &'a str| match input
|
||||
.char_indices()
|
||||
.take_while(|(_p, c)| pattern(*c))
|
||||
.last()
|
||||
{
|
||||
Some((index, c)) => {
|
||||
let index = index + c.len_utf8();
|
||||
Ok((&input[index..], &input[0..index]))
|
||||
}
|
||||
_ => Err(input),
|
||||
}
|
||||
}
|
||||
|
||||
// Variadic parser combinators
|
||||
|
||||
/// A parser combinator which matches a sequence of parsers in an all-or-nothing fashion.
|
||||
///
|
||||
/// The returned value is a tuple containing the outputs of all parsers in order. Each
|
||||
/// parser in the sequence may be typed differently.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{seq, Parser};
|
||||
/// let parser = seq!("<", "a", ">");
|
||||
/// assert_eq!(Ok(("", ("<", "a", ">"))), parser.parse("<a>"));
|
||||
/// assert_eq!(Err("<b>"), parser.parse("<b>"));
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! seq {
|
||||
($($parsers: expr),+ $(,)?) => {
|
||||
($($parsers),+)
|
||||
}
|
||||
}
|
||||
|
||||
// Seq is implemented using trait-implementations of Parser for various size tuples.
|
||||
// This allows sequences to be typed heterogeneously.
|
||||
macro_rules! seq_impl {
|
||||
($($parser:ident),+) => {
|
||||
#[allow(non_snake_case)]
|
||||
impl<'a, $($parser),+> Parser<'a> for ($($parser),+)
|
||||
where
|
||||
$($parser: Parser<'a>),+
|
||||
{
|
||||
type Output = ($($parser::Output),+);
|
||||
|
||||
fn parse(&self, input: &'a str) -> ParseResult<'a, Self::Output> {
|
||||
let ($($parser),+) = self;
|
||||
seq_body_impl!(input, input, $($parser),+ ; )
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! seq_body_impl {
|
||||
($input:expr, $next_input:expr, $head:ident, $($tail:ident),+ ; $(,)? $($acc:ident),*) => {
|
||||
match $head.parse($next_input) {
|
||||
Ok((next_input, $head)) => seq_body_impl!($input, next_input, $($tail),+ ; $($acc),*, $head),
|
||||
Err(_) => Err($input),
|
||||
}
|
||||
};
|
||||
($input:expr, $next_input:expr, $last:ident ; $(,)? $($acc:ident),*) => {
|
||||
match $last.parse($next_input) {
|
||||
Ok((next_input, last)) => Ok((next_input, ($($acc),+, last))),
|
||||
Err(_) => Err($input),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
seq_impl!(A, B);
|
||||
seq_impl!(A, B, C);
|
||||
seq_impl!(A, B, C, D);
|
||||
seq_impl!(A, B, C, D, E);
|
||||
seq_impl!(A, B, C, D, E, F);
|
||||
seq_impl!(A, B, C, D, E, F, G);
|
||||
seq_impl!(A, B, C, D, E, F, G, H);
|
||||
seq_impl!(A, B, C, D, E, F, G, H, I);
|
||||
seq_impl!(A, B, C, D, E, F, G, H, I, J);
|
||||
|
||||
/// A parser combinator which chooses the first of the input parsers which matches
|
||||
/// successfully.
|
||||
///
|
||||
/// All input parsers must have the same output type. This is a variadic form for [or].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{choice, or, Parser};
|
||||
/// let parser = choice!("foo", "bar", "baz");
|
||||
/// assert_eq!(Ok(("", "foo")), parser.parse("foo"));
|
||||
/// assert_eq!(Ok(("", "bar")), parser.parse("bar"));
|
||||
/// assert_eq!(Err("quiz"), parser.parse("quiz"));
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! choice {
|
||||
($parser: expr $(,)?) => {
|
||||
$parser
|
||||
};
|
||||
($parser: expr, $($rest: expr),+ $(,)?) => {
|
||||
or($parser, choice!($($rest),+))
|
||||
}
|
||||
}
|
||||
|
||||
// Ordinary parser combinators
|
||||
|
||||
/// A parser combinator which takes a parser as input and maps the output using the
|
||||
/// given transformation function.
|
||||
///
|
||||
/// This corresponds to [Result::map]. The value is only mapped if the input parser
|
||||
/// matches against input.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{map, Parser};
|
||||
/// let parser = map("123", |s| s.parse::<i32>().unwrap());
|
||||
/// assert_eq!(Ok(("", 123)), parser.parse("123"));
|
||||
/// assert_eq!(Err("abc"), parser.parse("abc"));
|
||||
/// ```
|
||||
pub fn map<'a, P, F, T>(parser: P, map_fn: F) -> impl Parser<'a, Output = T>
|
||||
where
|
||||
P: Parser<'a>,
|
||||
F: Fn(P::Output) -> T,
|
||||
{
|
||||
move |input| {
|
||||
parser
|
||||
.parse(input)
|
||||
.map(|(next_input, result)| (next_input, map_fn(result)))
|
||||
}
|
||||
}
|
||||
|
||||
/// A parser combinator which succeeds if the given parser matches the input and
|
||||
/// the given `filter_map_fn` returns `Some`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{filter_map, take_until, Parser};
|
||||
/// let parser = filter_map(take_until(|c| c == '.'), |s| s.parse::<i32>().ok());
|
||||
/// assert_eq!(Ok((".456", 123)), parser.parse("123.456"));
|
||||
/// assert_eq!(Err("abc.def"), parser.parse("abc.def"));
|
||||
/// ```
|
||||
pub fn filter_map<'a, P, F, T>(parser: P, filter_map_fn: F) -> impl Parser<'a, Output = T>
|
||||
where
|
||||
P: Parser<'a>,
|
||||
F: Fn(P::Output) -> Option<T>,
|
||||
{
|
||||
move |input| match parser.parse(input) {
|
||||
Ok((next_input, value)) => match filter_map_fn(value) {
|
||||
Some(value) => Ok((next_input, value)),
|
||||
None => Err(input),
|
||||
},
|
||||
Err(_) => Err(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// A parser combinator which succeeds if the first given parser matches the input and
|
||||
/// the second given parse also matches.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{reparse_as, take_until, one_or_more, Parser};
|
||||
/// let parser = reparse_as(take_until(|c| c == '/'), one_or_more("a"));
|
||||
/// assert_eq!(Ok(("/bb", vec!["a", "a"])), parser.parse("aa/bb"));
|
||||
/// ```
|
||||
pub fn reparse_as<'a, P1, P2, T>(parser1: P1, parser2: P2) -> impl Parser<'a, Output = T>
|
||||
where
|
||||
P1: Parser<'a, Output = &'a str>,
|
||||
P2: Parser<'a, Output = T>,
|
||||
{
|
||||
filter_map(parser1, move |str| {
|
||||
parser2.parse(str).map(|(_, value)| value).ok()
|
||||
})
|
||||
}
|
||||
|
||||
/// A parser combinator which only matches the input when the predicate function
|
||||
/// returns true.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{filter, take_until, Parser};
|
||||
/// let parser = filter(take_until(|c| c == '.'), |s| s == &"123");
|
||||
/// assert_eq!(Ok((".456", "123")), parser.parse("123.456"));
|
||||
/// assert_eq!(Err("456.123"), parser.parse("456.123"));
|
||||
/// ```
|
||||
pub fn filter<'a, P, F, T>(parser: P, pred_fn: F) -> impl Parser<'a, Output = T>
|
||||
where
|
||||
P: Parser<'a, Output = T>,
|
||||
F: Fn(&P::Output) -> bool,
|
||||
{
|
||||
move |input| {
|
||||
if let Ok((next_input, value)) = parser.parse(input) {
|
||||
if pred_fn(&value) {
|
||||
return Ok((next_input, value));
|
||||
}
|
||||
}
|
||||
Err(input)
|
||||
}
|
||||
}
|
||||
|
||||
/// A parser combinator which matches either of the input parsers.
|
||||
///
|
||||
/// Both parsers must have the same output type. For a variadic form which
|
||||
/// can take any number of parsers, use `choice!`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{or, Parser};
|
||||
/// let parser = or("foo", "bar");
|
||||
/// assert_eq!(Ok(("", "foo")), parser.parse("foo"));
|
||||
/// assert_eq!(Ok(("", "bar")), parser.parse("bar"));
|
||||
/// assert_eq!(Err("baz"), parser.parse("baz"));
|
||||
/// ```
|
||||
pub fn or<'a, P1, P2, T>(parser1: P1, parser2: P2) -> impl Parser<'a, Output = T>
|
||||
where
|
||||
P1: Parser<'a, Output = T>,
|
||||
P2: Parser<'a, Output = T>,
|
||||
{
|
||||
move |input| match parser1.parse(input) {
|
||||
ok @ Ok(_) => ok,
|
||||
Err(_) => parser2.parse(input),
|
||||
}
|
||||
}
|
||||
|
||||
/// A parser combinator which attempts to match the given parser, returning a
|
||||
/// `None` output value if the parser does not match.
|
||||
///
|
||||
/// The parser produced with this combinator always succeeds. If the given parser
|
||||
/// succeeds, `Some(value)` is returned where `value` is the output of the given
|
||||
/// parser. Otherwise, `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{optional, Parser};
|
||||
/// let parser = optional("foo");
|
||||
/// assert_eq!(Ok(("bar", Some("foo"))), parser.parse("foobar"));
|
||||
/// assert_eq!(Ok(("bar", None)), parser.parse("bar"));
|
||||
/// ```
|
||||
pub fn optional<'a, P, T>(parser: P) -> impl Parser<'a, Output = Option<T>>
|
||||
where
|
||||
P: Parser<'a, Output = T>,
|
||||
{
|
||||
move |input| match parser.parse(input) {
|
||||
Ok((next_input, value)) => Ok((next_input, Some(value))),
|
||||
Err(_) => Ok((input, None)),
|
||||
}
|
||||
}
|
||||
|
||||
/// A parser combinator which runs the given parsers in sequence and returns the
|
||||
/// value of `left` if both are matched.
|
||||
///
|
||||
/// This is useful for two-element sequences in which you only want the output
|
||||
/// value of the `left` parser.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{left, Parser};
|
||||
/// let parser = left("foo", "bar");
|
||||
/// assert_eq!(Ok(("", "foo")), parser.parse("foobar"));
|
||||
/// ```
|
||||
pub fn left<'a, L, R, T>(left: L, right: R) -> impl Parser<'a, Output = T>
|
||||
where
|
||||
L: Parser<'a, Output = T>,
|
||||
R: Parser<'a>,
|
||||
{
|
||||
map(seq!(left, right), |(left_value, _)| left_value)
|
||||
}
|
||||
|
||||
/// A parser combinator which runs the given parsers in sequence and returns the
|
||||
/// value of `right` if both are matched.
|
||||
///
|
||||
/// This is useful for two-element sequences in which you only want the output
|
||||
/// value of the `right` parser.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{right, Parser};
|
||||
/// let parser = right("foo", "bar");
|
||||
/// assert_eq!(Ok(("", "bar")), parser.parse("foobar"));
|
||||
/// ```
|
||||
pub fn right<'a, L, R, T>(left: L, right: R) -> impl Parser<'a, Output = T>
|
||||
where
|
||||
L: Parser<'a>,
|
||||
R: Parser<'a, Output = T>,
|
||||
{
|
||||
map(seq!(left, right), |(_, right_value)| right_value)
|
||||
}
|
||||
|
||||
/// A parser combinator which matches the given parser against the input zero or
|
||||
/// more times.
|
||||
///
|
||||
/// This parser always succeeds and returns the empty Vec when it matched zero
|
||||
/// times.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{zero_or_more, Parser};
|
||||
/// let parser = zero_or_more("a");
|
||||
/// assert_eq!(Ok(("", vec![])), parser.parse(""));
|
||||
/// assert_eq!(Ok(("", vec!["a"])), parser.parse("a"));
|
||||
/// assert_eq!(Ok(("", vec!["a", "a"])), parser.parse("aa"));
|
||||
/// assert_eq!(Ok(("bb", vec![])), parser.parse("bb"));
|
||||
/// ```
|
||||
pub fn zero_or_more<'a, P, T>(parser: P) -> impl Parser<'a, Output = Vec<T>>
|
||||
where
|
||||
P: Parser<'a, Output = T>,
|
||||
{
|
||||
move |mut input| {
|
||||
let mut values = Vec::new();
|
||||
|
||||
while let Ok((next_input, value)) = parser.parse(input) {
|
||||
input = next_input;
|
||||
values.push(value);
|
||||
}
|
||||
|
||||
Ok((input, values))
|
||||
}
|
||||
}
|
||||
|
||||
/// A parser combinator which matches the given parser against the input one or
|
||||
/// more times.
|
||||
///
|
||||
/// This parser combinator acts the same as [zero_or_more] but must match at
|
||||
/// least once.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use helix_parsec::{one_or_more, Parser};
|
||||
/// let parser = one_or_more("a");
|
||||
/// assert_eq!(Err(""), parser.parse(""));
|
||||
/// assert_eq!(Ok(("", vec!["a"])), parser.parse("a"));
|
||||
/// assert_eq!(Ok(("", vec!["a", "a"])), parser.parse("aa"));
|
||||
/// assert_eq!(Err("bb"), parser.parse("bb"));
|
||||
/// ```
|
||||
pub fn one_or_more<'a, P, T>(parser: P) -> impl Parser<'a, Output = Vec<T>>
|
||||
where
|
||||
P: Parser<'a, Output = T>,
|
||||
{
|
||||
move |mut input| {
|
||||
let mut values = Vec::new();
|
||||
|
||||
match parser.parse(input) {
|
||||
Ok((next_input, value)) => {
|
||||
input = next_input;
|
||||
values.push(value);
|
||||
}
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
|
||||
while let Ok((next_input, value)) = parser.parse(input) {
|
||||
input = next_input;
|
||||
values.push(value);
|
||||
}
|
||||
|
||||
Ok((input, values))
|
||||
}
|
||||
}
|
||||
|
||||
/// A parser combinator which matches one or more instances of the given parser
|
||||
/// interspersed with the separator parser.
|
||||
///
|
||||
/// Output values of the separator parser are discarded.
|
||||
///
|
||||
/// This is typically used to parse function arguments or list items.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use helix_parsec::{sep, Parser};
|
||||
/// let parser = sep("a", ",");
|
||||
/// assert_eq!(Ok(("", vec!["a", "a", "a"])), parser.parse("a,a,a"));
|
||||
/// ```
|
||||
pub fn sep<'a, P, S, T>(parser: P, separator: S) -> impl Parser<'a, Output = Vec<T>>
|
||||
where
|
||||
P: Parser<'a, Output = T>,
|
||||
S: Parser<'a>,
|
||||
{
|
||||
move |mut input| {
|
||||
let mut values = Vec::new();
|
||||
|
||||
match parser.parse(input) {
|
||||
Ok((next_input, value)) => {
|
||||
input = next_input;
|
||||
values.push(value);
|
||||
}
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
|
||||
loop {
|
||||
match separator.parse(input) {
|
||||
Ok((next_input, _)) => input = next_input,
|
||||
Err(_) => break,
|
||||
}
|
||||
|
||||
match parser.parse(input) {
|
||||
Ok((next_input, value)) => {
|
||||
input = next_input;
|
||||
values.push(value);
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
|
||||
Ok((input, values))
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue