rename Node and NodeValue

This commit is contained in:
Kat Marchán 2020-12-13 22:31:30 -08:00
parent e715b08800
commit 42564a7cf7
No known key found for this signature in database
GPG Key ID: AEB529C08A3C7E9E
3 changed files with 30 additions and 30 deletions

View File

@ -2,13 +2,13 @@ use nom::combinator::all_consuming;
use nom::Err; use nom::Err;
pub use crate::error::{KdlError, KdlErrorKind}; pub use crate::error::{KdlError, KdlErrorKind};
pub use crate::node::Node; pub use crate::node::KdlNode;
mod error; mod error;
mod node; mod node;
mod parser; mod parser;
pub fn parse_document<I>(input: I) -> Result<Vec<Node>, KdlError> pub fn parse_document<I>(input: I) -> Result<Vec<KdlNode>, KdlError>
where where
I: AsRef<str>, I: AsRef<str>,
{ {

View File

@ -1,15 +1,15 @@
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct Node { pub struct KdlNode {
pub name: String, pub name: String,
pub values: Vec<NodeValue>, pub values: Vec<KdlNodeValue>,
pub properties: HashMap<String, NodeValue>, pub properties: HashMap<String, KdlNodeValue>,
pub children: Vec<Node>, pub children: Vec<KdlNode>,
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum NodeValue { pub enum KdlNodeValue {
Int(i64), Int(i64),
Float(f64), Float(f64),
String(String), String(String),

View File

@ -9,21 +9,21 @@ use nom::sequence::{delimited, pair, preceded, terminated, tuple};
use nom::IResult; use nom::IResult;
use crate::error::KdlParseError; use crate::error::KdlParseError;
use crate::node::{Node, NodeValue}; use crate::node::{KdlNode, KdlNodeValue};
/// `nodes := linespace* (node (newline document)?)?` /// `nodes := linespace* (node (newline document)?)?`
pub(crate) fn nodes(input: &str) -> IResult<&str, Vec<Node>, KdlParseError<&str>> { pub(crate) fn nodes(input: &str) -> IResult<&str, Vec<KdlNode>, KdlParseError<&str>> {
many0(delimited(many0(linespace), node, newline))(input) many0(delimited(many0(linespace), node, newline))(input)
} }
#[derive(Clone)] #[derive(Clone)]
enum NodeArg { enum NodeArg {
Value(NodeValue), Value(KdlNodeValue),
Property(String, NodeValue), Property(String, KdlNodeValue),
} }
/// `node := identifier (node-space node-argument)* (node-space node-document)?` /// `node := identifier (node-space node-argument)* (node-space node-document)?`
pub(crate) fn node(input: &str) -> IResult<&str, Node, KdlParseError<&str>> { pub(crate) fn node(input: &str) -> IResult<&str, KdlNode, KdlParseError<&str>> {
let (input, tag) = identifier(input)?; let (input, tag) = identifier(input)?;
let (input, args) = many0(preceded(node_space, node_arg))(input)?; let (input, args) = many0(preceded(node_space, node_arg))(input)?;
let (input, children) = opt(preceded(node_space, node_children))(input)?; let (input, children) = opt(preceded(node_space, node_children))(input)?;
@ -32,7 +32,7 @@ pub(crate) fn node(input: &str) -> IResult<&str, Node, KdlParseError<&str>> {
.partition(|arg| matches!(arg, NodeArg::Value(_))); .partition(|arg| matches!(arg, NodeArg::Value(_)));
Ok(( Ok((
input, input,
Node { KdlNode {
name: tag, name: tag,
children: children.unwrap_or_else(Vec::new), children: children.unwrap_or_else(Vec::new),
values: values values: values
@ -77,7 +77,7 @@ fn node_arg(input: &str) -> IResult<&str, NodeArg, KdlParseError<&str>> {
} }
/// `prop := identifier '=' value` /// `prop := identifier '=' value`
fn property(input: &str) -> IResult<&str, (String, NodeValue), KdlParseError<&str>> { fn property(input: &str) -> IResult<&str, (String, KdlNodeValue), KdlParseError<&str>> {
let (input, key) = identifier(input)?; let (input, key) = identifier(input)?;
let (input, _) = tag("=")(input)?; let (input, _) = tag("=")(input)?;
let (input, val) = node_value(input)?; let (input, val) = node_value(input)?;
@ -85,18 +85,18 @@ fn property(input: &str) -> IResult<&str, (String, NodeValue), KdlParseError<&st
} }
/// `value := string | raw_string | number | boolean | 'null'` /// `value := string | raw_string | number | boolean | 'null'`
fn node_value(input: &str) -> IResult<&str, NodeValue, KdlParseError<&str>> { fn node_value(input: &str) -> IResult<&str, KdlNodeValue, KdlParseError<&str>> {
alt(( alt((
map(string, NodeValue::String), map(string, KdlNodeValue::String),
map(raw_string, |s| NodeValue::String(s.into())), map(raw_string, |s| KdlNodeValue::String(s.into())),
number, number,
boolean, boolean,
value(NodeValue::Null, tag("null")), value(KdlNodeValue::Null, tag("null")),
))(input) ))(input)
} }
/// `node-children := '{' nodes '}'` /// `node-children := '{' nodes '}'`
fn node_children(input: &str) -> IResult<&str, Vec<Node>, KdlParseError<&str>> { fn node_children(input: &str) -> IResult<&str, Vec<KdlNode>, KdlParseError<&str>> {
delimited(tag("{"), nodes, tag("}"))(input) delimited(tag("{"), nodes, tag("}"))(input)
} }
@ -156,13 +156,13 @@ fn raw_string(input: &str) -> IResult<&str, &str, KdlParseError<&str>> {
} }
/// `number := decimal | hex | octal | binary` /// `number := decimal | hex | octal | binary`
fn number(input: &str) -> IResult<&str, NodeValue, KdlParseError<&str>> { fn number(input: &str) -> IResult<&str, KdlNodeValue, KdlParseError<&str>> {
alt(( alt((
map(integer, NodeValue::Int), map(integer, KdlNodeValue::Int),
map(hexadecimal, NodeValue::Int), map(hexadecimal, KdlNodeValue::Int),
map(octal, NodeValue::Int), map(octal, KdlNodeValue::Int),
map(binary, NodeValue::Int), map(binary, KdlNodeValue::Int),
map(float, NodeValue::Float), map(float, KdlNodeValue::Float),
))(input) ))(input)
} }
@ -250,10 +250,10 @@ fn binary(input: &str) -> IResult<&str, i64, KdlParseError<&str>> {
} }
/// `boolean := 'true' | 'false'` /// `boolean := 'true' | 'false'`
fn boolean(input: &str) -> IResult<&str, NodeValue, KdlParseError<&str>> { fn boolean(input: &str) -> IResult<&str, KdlNodeValue, KdlParseError<&str>> {
alt(( alt((
value(NodeValue::Boolean(true), tag("true")), value(KdlNodeValue::Boolean(true), tag("true")),
value(NodeValue::Boolean(false), tag("false")), value(KdlNodeValue::Boolean(false), tag("false")),
))(input) ))(input)
} }
@ -412,8 +412,8 @@ mod tests {
#[test] #[test]
fn test_boolean() { fn test_boolean() {
assert_eq!(boolean("true"), Ok(("", NodeValue::Boolean(true)))); assert_eq!(boolean("true"), Ok(("", KdlNodeValue::Boolean(true))));
assert_eq!(boolean("false"), Ok(("", NodeValue::Boolean(false)))); assert_eq!(boolean("false"), Ok(("", KdlNodeValue::Boolean(false))));
assert!(boolean("blah").is_err()); assert!(boolean("blah").is_err());
} }