Merge branch 'nom2' into nom4

This commit is contained in:
Yehuda Katz
2019-06-22 18:20:13 -04:00
33 changed files with 113 additions and 127 deletions

View File

@ -1,6 +1,7 @@
use crate::parser::{hir, RawToken, Token};
use crate::Text;
pub fn baseline_parse_single_token(token: &Token, source: &str) -> hir::Expression {
pub fn baseline_parse_single_token(token: &Token, source: &Text) -> hir::Expression {
match *token.item() {
RawToken::Integer(int) => hir::Expression::int(int, token.span),
RawToken::Size(int, unit) => hir::Expression::size(int, unit, token.span),

View File

@ -1,11 +1,12 @@
use crate::errors::ShellError;
use crate::parser::registry::CommandRegistry;
use crate::parser::{hir, hir::baseline_parse_single_token, Span, Spanned, TokenNode};
use crate::Text;
pub fn baseline_parse_tokens(
token_nodes: &[TokenNode],
registry: &dyn CommandRegistry,
source: &str,
source: &Text,
) -> Result<Vec<hir::Expression>, ShellError> {
let mut exprs: Vec<hir::Expression> = vec![];
let mut rest = token_nodes;
@ -36,7 +37,7 @@ pub enum ExpressionKindHint {
pub fn baseline_parse_next_expr(
token_nodes: &'nodes [TokenNode],
_registry: &dyn CommandRegistry,
source: &str,
source: &Text,
coerce_hint: Option<ExpressionKindHint>,
) -> Result<(hir::Expression, &'nodes [TokenNode]), ShellError> {
let mut tokens = token_nodes.iter().peekable();
@ -143,7 +144,7 @@ pub fn baseline_parse_next_expr(
pub fn baseline_parse_semantic_token(
token: &TokenNode,
source: &str,
source: &Text,
) -> Result<hir::Expression, ShellError> {
match token {
TokenNode::Token(token) => Ok(baseline_parse_single_token(token, source)),

View File

@ -1,3 +1,4 @@
use crate::Text;
use derive_new::new;
use getset::Getters;
@ -40,8 +41,8 @@ impl<T> Spanned<T> {
}
}
pub fn source(&self, source: &'source str) -> &'source str {
self.span().slice(source)
pub fn source(&self, source: &Text) -> Text {
Text::from(self.span().slice(source))
}
}
@ -95,7 +96,7 @@ impl From<&std::ops::Range<usize>> for Span {
}
impl Span {
pub fn slice(&self, source: &'source str) -> &'source str {
pub fn slice(&self, source: &'a str) -> &'a str {
&source[self.start..self.end]
}
}

View File

@ -31,7 +31,7 @@ impl Text {
/// Extract a new `Text` that is a subset of an old `Text`
/// -- `text.extract(1..3)` is similar to `&foo[1..3]` except that
/// it gives back an owned value instead of a borrowed value.
pub fn extract(&self, range: Range<usize>) -> Self {
pub fn slice(&self, range: Range<usize>) -> Self {
let mut result = self.clone();
result.select(range);
result

View File

@ -1,5 +1,6 @@
use crate::errors::ShellError;
use crate::parser::parse2::{call_node::*, flag::*, operator::*, span::*, tokens::*};
use crate::Text;
use derive_new::new;
use enum_utils::FromStr;
use getset::Getters;
@ -36,11 +37,11 @@ impl TokenNode {
}
}
pub fn as_external_arg(&self, source: &str) -> String {
pub fn as_external_arg(&self, source: &Text) -> String {
self.span().slice(source).to_string()
}
pub fn source(&self, source: &'source str) -> &'source str {
pub fn source(&self, source: &'a Text) -> &'a str {
self.span().slice(source)
}
@ -54,7 +55,7 @@ impl TokenNode {
}
}
crate fn as_flag(&self, value: &str, source: &str) -> Option<Spanned<Flag>> {
crate fn as_flag(&self, value: &str, source: &Text) -> Option<Spanned<Flag>> {
match self {
TokenNode::Flag(
flag @ Spanned {
@ -99,19 +100,3 @@ pub struct PipelineElement {
call: Spanned<CallNode>,
pub post_ws: Option<Span>,
}
impl PipelineElement {
crate fn span(&self) -> Span {
let start = match self.pre_ws {
None => self.call.span.start,
Some(span) => span.start,
};
let end = match self.post_ws {
None => self.call.span.end,
Some(span) => span.end,
};
Span::from((start, end))
}
}

View File

@ -11,28 +11,3 @@ pub enum RawToken {
}
pub type Token = Spanned<RawToken>;
impl Token {
pub fn to_semantic_token(&self) -> Option<SemanticToken> {
let semantic_token = match self.item {
RawToken::Integer(int) => RawSemanticToken::Integer(int),
RawToken::Size(int, unit) => RawSemanticToken::Size(int, unit),
RawToken::String(span) => RawSemanticToken::String(span),
RawToken::Variable(span) => RawSemanticToken::Variable(span),
RawToken::Bare => RawSemanticToken::Bare,
};
Some(Spanned::from_item(semantic_token, self.span))
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum RawSemanticToken {
Integer(i64),
Size(i64, Unit),
String(Span),
Variable(Span),
Bare,
}
pub type SemanticToken = Spanned<RawSemanticToken>;

View File

@ -5,13 +5,14 @@ use crate::parser::{
hir::{self, NamedArguments},
Flag, RawToken, TokenNode,
};
use crate::Text;
use log::trace;
pub fn parse_command(
config: &CommandConfig,
registry: &dyn CommandRegistry,
call: &Spanned<CallNode>,
source: &str,
source: &Text,
) -> Result<hir::Call, ShellError> {
let Spanned { item: call, .. } = call;
@ -64,7 +65,7 @@ fn parse_command_tail(
config: &CommandConfig,
registry: &dyn CommandRegistry,
tail: Option<Vec<TokenNode>>,
source: &str,
source: &Text,
) -> Result<Option<(Option<Vec<hir::Expression>>, Option<NamedArguments>)>, ShellError> {
let mut tail = match tail {
None => return Ok(None),
@ -176,7 +177,7 @@ fn parse_command_tail(
fn extract_switch(
name: &str,
mut tokens: Vec<TokenNode>,
source: &str,
source: &Text,
) -> (Vec<TokenNode>, Option<Flag>) {
let pos = tokens
.iter()
@ -196,7 +197,7 @@ fn extract_switch(
fn extract_mandatory(
name: &str,
mut tokens: Vec<TokenNode>,
source: &str,
source: &Text,
) -> Result<(Vec<TokenNode>, usize, Flag), ShellError> {
let pos = tokens
.iter()
@ -225,7 +226,7 @@ fn extract_mandatory(
fn extract_optional(
name: &str,
mut tokens: Vec<TokenNode>,
source: &str,
source: &Text,
) -> Result<(Vec<TokenNode>, Option<(usize, Flag)>), ShellError> {
let pos = tokens
.iter()

View File

@ -189,7 +189,7 @@ impl CommandConfig {
call: &Spanned<CallNode>,
registry: &dyn CommandRegistry,
scope: &Scope,
source: &str,
source: &Text,
) -> Result<Args, ShellError> {
let args = parse_command(self, registry, call, source)?;
@ -282,7 +282,7 @@ fn evaluate_args(
args: hir::Call,
registry: &dyn CommandRegistry,
scope: &Scope,
source: &str,
source: &Text,
) -> Result<Args, ShellError> {
let positional: Result<Option<Vec<_>>, _> = args
.positional()