Skip to content
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ categories = ["command-line-interface"]
build = "build.rs"

[dependencies]
nom = { version = "7", default-features = false, features = ["std"] }
nom = { version = "8", default-features = false, features = ["std"] }
phf = "0.11"
fnv = "1.0"

Expand Down
46 changes: 25 additions & 21 deletions src/parser/compiled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use nom::bytes::streaming::{tag, take, take_until};
use nom::combinator::{complete, cond, map, map_opt, map_parser, opt};
use nom::multi::count;
use nom::number::streaming::{le_i16, le_i32};
use nom::IResult;
use nom::{IResult, Parser};
use std::str;

use crate::capability::Value;
Expand Down Expand Up @@ -126,53 +126,55 @@ fn bit_size(magic: &[u8]) -> usize {
}

pub fn parse(input: &[u8]) -> IResult<&[u8], Database<'_>> {
let (input, magic) = alt((tag([0x1A, 0x01]), tag([0x1E, 0x02])))(input)?;
let (input, magic) = alt((tag(&[0x1A, 0x01][..]), tag(&[0x1E, 0x02][..]))).parse(input)?;

let (input, name_size) = size(input)?;
let (input, bool_count) = size(input)?;
let (input, num_count) = size(input)?;
let (input, string_count) = size(input)?;
let (input, table_size) = size(input)?;

let (input, names) = map_parser(take(name_size), take_until("\x00"))(input)?;
let (input, names) = map_parser(take(name_size), take_until("\x00")).parse(input)?;

let (input, booleans) = count(boolean, bool_count)(input)?;
let (input, booleans) = count(boolean, bool_count).parse(input)?;

let (input, _) = cond((name_size + bool_count) % 2 != 0, take(1_usize))(input)?;
let (input, _) = cond((name_size + bool_count) % 2 != 0, take(1_usize)).parse(input)?;

let (input, numbers) = count(|input| capability(input, bit_size(magic)), num_count)(input)?;
let (input, numbers) =
count(|input| capability(input, bit_size(magic)), num_count).parse(input)?;

let (input, strings) = count(|input| capability(input, 16), string_count)(input)?;
let (input, strings) = count(|input| capability(input, 16), string_count).parse(input)?;

let (input, table) = take(table_size)(input)?;

let (input, extended) = opt(complete(|input| {
let (input, _) = cond(table_size % 2 != 0, take(1_usize))(input)?;
let (input, _) = cond(table_size % 2 != 0, take(1_usize)).parse(input)?;

let (input, ext_bool_count) = size(input)?;
let (input, ext_num_count) = size(input)?;
let (input, ext_string_count) = size(input)?;
let (input, _ext_offset_count) = size(input)?;
let (input, ext_table_size) = size(input)?;

let (input, booleans) = count(boolean, ext_bool_count)(input)?;
let (input, booleans) = count(boolean, ext_bool_count).parse(input)?;

let (input, _) = cond(ext_bool_count % 2 != 0, take(1_usize))(input)?;
let (input, _) = cond(ext_bool_count % 2 != 0, take(1_usize)).parse(input)?;

let (input, numbers) =
count(|input| capability(input, bit_size(magic)), ext_num_count)(input)?;
count(|input| capability(input, bit_size(magic)), ext_num_count).parse(input)?;

let (input, strings) = count(|input| capability(input, 16), ext_string_count)(input)?;
let (input, strings) =
count(|input| capability(input, 16), ext_string_count).parse(input)?;

let (input, names) = count(
|input| capability(input, 16),
ext_bool_count + ext_num_count + ext_string_count,
)(input)?;
let (input, names) =
count(|input| capability(input, 16), ext_bool_count + ext_num_count + ext_string_count)
.parse(input)?;

let (input, table) = take(ext_table_size)(input)?;
let (input, table) = take(ext_table_size).parse(input)?;

Ok((input, Extended { booleans, numbers, strings, names, table }))
}))(input)?;
}))
.parse(input)?;

Ok((
input,
Expand All @@ -181,15 +183,16 @@ pub fn parse(input: &[u8]) -> IResult<&[u8], Database<'_>> {
}

fn boolean(input: &[u8]) -> IResult<&[u8], bool> {
alt((map(tag([0]), |_| false), map(tag([1]), |_| true)))(input)
alt((map(tag(&[0][..]), |_| false), map(tag(&[1][..]), |_| true))).parse(input)
}

fn size(input: &[u8]) -> IResult<&[u8], usize> {
map_opt(le_i16, |n| match n {
-1 => Some(0),
n if n >= 0 => Some(n as usize),
_ => None,
})(input)
})
.parse(input)
}

fn capability(input: &[u8], bits: usize) -> IResult<&[u8], i32> {
Expand All @@ -199,7 +202,8 @@ fn capability(input: &[u8], bits: usize) -> IResult<&[u8], i32> {
|o| o,
),
map_opt(cond(bits == 32, map_opt(le_i32, |n| if n >= -2 { Some(n) } else { None })), |o| o),
))(input)
))
.parse(input)
}

#[cfg(test)]
Expand Down
34 changes: 19 additions & 15 deletions src/parser/expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ use crate::parser::util::number;
use nom::branch::alt;
use nom::bytes::complete;
use nom::bytes::streaming::{tag, take, take_while};
use nom::character::is_digit;
use nom::character::streaming::one_of;
use nom::combinator::{map, opt, value};
use nom::error::{make_error, ErrorKind};
use nom::IResult;
use nom::{AsChar, IResult, Parser};

#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum Item<'a> {
Expand Down Expand Up @@ -116,26 +115,27 @@ pub struct Flags {
}

pub fn parse(input: &[u8]) -> IResult<&[u8], Item<'_>> {
alt((expansion, string))(input)
alt((expansion, string)).parse(input)
}

fn string(input: &[u8]) -> IResult<&[u8], Item<'_>> {
map(complete::take_till(|b| b == b'%'), Item::String)(input)
map(complete::take_till(|b| b == b'%'), Item::String).parse(input)
}

fn expansion(input: &[u8]) -> IResult<&[u8], Item<'_>> {
let (input, _) = tag("%")(input)?;
let (input, item) = alt((percent, constant, variable, operation, conditional, print))(input)?;
let (input, item) =
alt((percent, constant, variable, operation, conditional, print)).parse(input)?;

Ok((input, item))
}

fn percent(input: &[u8]) -> IResult<&[u8], Item<'_>> {
value(Item::String(b"%"), tag("%"))(input)
value(Item::String(b"%"), tag("%")).parse(input)
}

fn constant(input: &[u8]) -> IResult<&[u8], Item<'_>> {
alt((constant_char, constant_integer))(input)
alt((constant_char, constant_integer)).parse(input)
}

fn constant_char(input: &[u8]) -> IResult<&[u8], Item<'_>> {
Expand All @@ -148,7 +148,7 @@ fn constant_char(input: &[u8]) -> IResult<&[u8], Item<'_>> {

fn constant_integer(input: &[u8]) -> IResult<&[u8], Item<'_>> {
let (input, _) = tag("{")(input)?;
let (input, digit) = take_while(is_digit)(input)?;
let (input, digit) = take_while(AsChar::is_dec_digit)(input)?;
let (input, _) = tag("}")(input)?;

Ok((input, Item::Constant(Constant::Integer(number(digit)))))
Expand All @@ -159,7 +159,8 @@ fn variable(input: &[u8]) -> IResult<&[u8], Item<'_>> {
match c {
b"l" => Ok((input, Item::Variable(Variable::Length))),

b"p" => map(one_of("123456789"), |n| Item::Variable(Variable::Push(n as u8 - b'1')))(input),
b"p" => map(one_of("123456789"), |n| Item::Variable(Variable::Push(n as u8 - b'1')))
.parse(input),

b"P" => alt((
map(one_of("abcdefghijklmnopqrstuvwxyz"), |n| {
Expand All @@ -168,7 +169,8 @@ fn variable(input: &[u8]) -> IResult<&[u8], Item<'_>> {
map(one_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), |n| {
Item::Variable(Variable::Set(false, n as u8 - b'A'))
}),
))(input),
))
.parse(input),

b"g" => alt((
map(one_of("abcdefghijklmnopqrstuvwxyz"), |n| {
Expand All @@ -177,7 +179,8 @@ fn variable(input: &[u8]) -> IResult<&[u8], Item<'_>> {
map(one_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), |n| {
Item::Variable(Variable::Get(false, n as u8 - b'A'))
}),
))(input),
))
.parse(input),

_ => Err(nom::Err::Error(make_error(input, ErrorKind::Switch))),
}
Expand Down Expand Up @@ -223,16 +226,17 @@ fn conditional(input: &[u8]) -> IResult<&[u8], Item<'_>> {
}

fn print(input: &[u8]) -> IResult<&[u8], Item<'_>> {
let (input, _) = opt(tag(":"))(input)?;
let (input, _) = opt(tag(":")).parse(input)?;

let (input, flags) = take_while(is_flag)(input)?;
let (input, width) = opt(take_while(is_digit))(input)?;
let (input, width) = opt(take_while(AsChar::is_dec_digit)).parse(input)?;
let (input, precision) = opt(|input| {
let (input, _) = tag(".")(input)?;
let (input, amount) = take_while(is_digit)(input)?;
let (input, amount) = take_while(AsChar::is_dec_digit).parse(input)?;

Ok((input, amount))
})(input)?;
})
.parse(input)?;

let (input, format) = one_of("doxXsc")(input)?;

Expand Down
44 changes: 23 additions & 21 deletions tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
use nom::branch::alt;
use nom::bytes::streaming::{tag, take, take_until, take_while};
use nom::character::streaming::char;
use nom::character::{is_digit, streaming::line_ending as eol};
use nom::character::streaming::line_ending as eol;
use nom::combinator::{complete, eof, map, map_res, opt};
use nom::error::{make_error, ErrorKind};
use nom::sequence::terminated;
use nom::IResult;
use nom::{AsChar, IResult, Parser};
use std::borrow::Cow;
use std::fs::File;
use std::io::Read;
Expand Down Expand Up @@ -96,11 +96,11 @@ pub fn is_printable_no_control(ch: u8) -> bool {
}

pub fn ws(input: &[u8]) -> IResult<&[u8], char> {
alt((char(' '), char('\t')))(input)
alt((char(' '), char('\t'))).parse(input)
}

pub fn end(input: &[u8]) -> IResult<&[u8], &[u8]> {
alt((eof, eol))(input)
alt((eof, eol)).parse(input)
}

pub fn unescape(i: &[u8]) -> Cow<'_, [u8]> {
Expand Down Expand Up @@ -134,8 +134,8 @@ pub fn unescape(i: &[u8]) -> Cow<'_, [u8]> {

Some(b'0') => output.push(0x00),

Some(a) if is_digit(a) => match (iter.next(), iter.next()) {
(Some(b), Some(c)) if is_digit(b) && is_digit(c) => {
Some(a) if AsChar::is_dec_digit(a) => match (iter.next(), iter.next()) {
(Some(b), Some(c)) if AsChar::is_dec_digit(b) && AsChar::is_dec_digit(c) => {
if let Ok(number) =
u8::from_str_radix(unsafe { str::from_utf8_unchecked(&[a, b, c]) }, 8)
{
Expand Down Expand Up @@ -215,32 +215,35 @@ pub enum Item<'a> {
}

pub fn parse(input: &[u8]) -> IResult<&[u8], Item<'_>> {
alt((comment, definition, disable, entry))(input)
alt((comment, definition, disable, entry)).parse(input)
}

fn comment(input: &[u8]) -> IResult<&[u8], Item<'_>> {
let (input, _) = tag("#")(input)?;
let (input, content) = map_res(terminated(take_until("\n"), tag("\n")), str::from_utf8)(input)?;
let (input, _) = opt(complete(take_while(is_eol)))(input)?;
let (input, content) =
map_res(terminated(take_until("\n"), tag("\n")), str::from_utf8).parse(input)?;
let (input, _) = opt(complete(take_while(is_eol))).parse(input)?;

Ok((input, Item::Comment(content.trim())))
}

fn definition(input: &[u8]) -> IResult<&[u8], Item<'_>> {
let (input, name) =
map(take_while(is_printable_no_pipe), |n| unsafe { str::from_utf8_unchecked(n) })(input)?;
map(take_while(is_printable_no_pipe), |n| unsafe { str::from_utf8_unchecked(n) })
.parse(input)?;

let (input, _) = tag("|")(input)?;

let (input, content) =
map(take_while(is_printable_no_comma), |n| unsafe { str::from_utf8_unchecked(n) })(input)?;
map(take_while(is_printable_no_comma), |n| unsafe { str::from_utf8_unchecked(n) })
.parse(input)?;

let (input, _) = tag(",")(input)?;

let (input, _) = take_while(is_ws)(input)?;

let (input, _) = eol(input)?;
let (input, _) = opt(complete(take_while(is_eol)))(input)?;
let (input, _) = opt(complete(take_while(is_eol))).parse(input)?;

Ok((input, {
let mut aliases = content.split('|').map(|n| n.trim()).collect::<Vec<_>>();
Expand All @@ -255,14 +258,13 @@ fn disable(input: &[u8]) -> IResult<&[u8], Item<'_>> {
let (input, _) = tag("@")(input)?;

let (input, name) =
map(take_while(is_printable_no_control), |n| unsafe { str::from_utf8_unchecked(n) })(
input,
)?;
map(take_while(is_printable_no_control), |n| unsafe { str::from_utf8_unchecked(n) })
.parse(input)?;

let (input, _) = tag(",")(input)?;
let (input, _) = take_while(is_ws)(input)?;
let (input, _) = end(input)?;
let (input, _) = opt(complete(take_while(is_eol)))(input)?;
let (input, _) = opt(complete(take_while(is_eol))).parse(input)?;

Ok((input, Item::Disable(name)))
}
Expand All @@ -272,17 +274,17 @@ fn entry(input: &[u8]) -> IResult<&[u8], Item<'_>> {
let (input, _) = take_while(is_ws)(input)?;

let (input, name) =
map(take_while(is_printable_no_control), |n| unsafe { str::from_utf8_unchecked(n) })(
input,
)?;
map(take_while(is_printable_no_control), |n| unsafe { str::from_utf8_unchecked(n) })
.parse(input)?;

let (input, c) = take(1_usize)(input)?;
let (input, value) = match c {
b"," => (input, Item::True(name)),

b"#" => {
let (input, value) =
map(take_while(is_digit), |n| unsafe { str::from_utf8_unchecked(n) })(input)?;
map(take_while(AsChar::is_dec_digit), |n| unsafe { str::from_utf8_unchecked(n) })
.parse(input)?;

let (input, _) = tag(",")(input)?;

Expand All @@ -302,7 +304,7 @@ fn entry(input: &[u8]) -> IResult<&[u8], Item<'_>> {

let (input, _) = take_while(is_ws)(input)?;
let (input, _) = end(input)?;
let (input, _) = opt(complete(take_while(is_eol)))(input)?;
let (input, _) = opt(complete(take_while(is_eol))).parse(input)?;

Ok((input, value))
}
Expand Down