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
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: rust
branches:
only:
- master
matrix:
include:
- name: "stable rust"
rust: stable
before_script:
- rustup component add rustfmt
script:
- cargo fmt -- --check
- cargo build
- cargo test
- name: "nightly rust + rustfmt"
rust: nightly
before_script:
- rustup component add rustfmt --toolchain nightly-x86_64-unknown-linux-gnu
script:
- cargo fmt -- --check
- cargo build
- cargo test
allow_failures:
- name: "stable rust"
19 changes: 17 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,20 @@ name = "molysite"
version = "0.1.0"
edition = "2018"

[dependencies]
nom = "^3.2"
[dependencies.nom]
version = "^4.2"

[dependencies.serde_json]
optional = true
version = "1.0.9"

[dev-dependencies]
serde_derive = "1.0.27"

[dev-dependencies.serde]
version = "1.0.27"

[features]
arraynested = []
withserde = ["serde_json"]
default = ["arraynested"]
6 changes: 2 additions & 4 deletions examples/hcl2json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ fn main() {
let mut file = match File::open(&path) {
// The `description` method of `io::Error` returns a string that
// describes the error
Err(why) => panic!("couldn't open {}: {}", display,
why.description()),
Err(why) => panic!("couldn't open {}: {}", display, why.description()),
Ok(file) => file,
};

// Read the file contents into a string, returns `io::Result<usize>`
let mut s = String::new();
match file.read_to_string(&mut s) {
Err(why) => panic!("couldn't read {}: {}", display,
why.description()),
Err(why) => panic!("couldn't read {}: {}", display, why.description()),
Ok(_) => {
let parsed = parse_hcl(&s);
print!("{}\n", parsed.unwrap());
Expand Down
77 changes: 47 additions & 30 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
use std::num::ParseIntError;
use std::str::{self, FromStr};
use std::u32;

use nom;
use nom::types::CompleteStr;
use nom::{digit, hex_digit};

named!(pub boolean<bool>,
map!(
alt_complete!(tag!("true") | tag!("false")),
|value: &[u8]| value == b"true"
)
#[macro_export]
macro_rules! complete_named (
($name:ident, $submac:ident!( $($args:tt)* )) => (
fn $name<'a>( i: CompleteStr<'a> ) -> nom::IResult<CompleteStr<'a>, CompleteStr<'a>, u32> {
$submac!(i, $($args)*)
}
);
($name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => (
fn $name<'a>( i: CompleteStr<'a> ) -> nom::IResult<CompleteStr<'a>, $o, u32> {
$submac!(i, $($args)*)
}
);
(pub $name:ident, $submac:ident!( $($args:tt)* )) => (
pub fn $name<'a>( i: CompleteStr<'a> ) -> nom::IResult<CompleteStr<'a>, CompleteStr<'a>, u32> {
$submac!(i, $($args)*)
}
);
(pub $name:ident<$o:ty>, $submac:ident!( $($args:tt)* )) => (
pub fn $name<'a>( i: CompleteStr<'a> ) -> nom::IResult<CompleteStr<'a>, $o, u32> {
$submac!(i, $($args)*)
}
);
);

named!(
complete_named!(pub boolean<bool>,map!(
alt!(tag!("true") | tag!("false")),
|value: CompleteStr| value == CompleteStr("true")
));

complete_named!(
unsigned_float,
recognize!(alt_complete!(
delimited!(digit, tag!("."), opt!(complete!(digit)))
Expand All @@ -20,35 +43,29 @@ named!(
))
);

named!(pub float<f32>, map_res!(
map_res!(
recognize!(alt_complete!(
delimited!(
pair!(opt!(alt!(tag!("+") | tag!("-"))), unsigned_float),
tag!("e"),
pair!(opt!(alt!(tag!("+") | tag!("-"))), unsigned_float)
) |
unsigned_float
)),
str::from_utf8
),
FromStr::from_str
complete_named!(pub float<f32>, flat_map!(
recognize!(alt_complete!(
delimited!(
pair!(opt!(alt!(tag!("+") | tag!("-"))), unsigned_float),
tag!("e"),
pair!(opt!(alt!(tag!("+") | tag!("-"))), unsigned_float)
) |
unsigned_float
)),
parse_to!(f32)
));

fn to_i(i: &str) -> Result<u32, ParseIntError> {
u32::from_str_radix(i, 16)
fn complete_to_i(i: CompleteStr) -> Result<u32, ParseIntError> {
u32::from_str_radix(i.0, 16)
}

named!(pub int<u32>, map_res!(
map_res!(
preceded!(tag!("0x"), hex_digit),
str::from_utf8
),
to_i
));
complete_named!(
int<u32>,
map_res!(preceded!(tag!("0x"), hex_digit), complete_to_i)
);

// TODO: add support for octal
named!(pub number<f32>, alt_complete!(
complete_named!(pub number<f32>, alt_complete!(
map!(int, |i| { i as f32 }) |
float
));
Loading