FEATURE: Parsing of comments.

This commit is contained in:
Jeremy Wall 2017-12-24 15:24:06 -05:00
parent 4c2528fe07
commit 336dd5e5ea
7 changed files with 91 additions and 27 deletions

View File

@ -160,3 +160,10 @@ We also support a limited macro facility with the macro expression.
Macro's always output a tuple whose fields are evaluated at the location they Macro's always output a tuple whose fields are evaluated at the location they
are called from. You can acccess the generated fields from the resulting tuple are called from. You can acccess the generated fields from the resulting tuple
like usual. like usual.
### Comments
Comments begin with `//` and go till the end of the line.
// This is a comment.
let one = 1;

View File

@ -16,6 +16,5 @@ organiztion for a given configuration structure. Some options here could be
# Minor Fixes and Polish # Minor Fixes and Polish
* Comment syntax
* JSON export * JSON export
* YAML export * YAML export

View File

@ -1,7 +1,9 @@
// A few constants.
let dbhost1 = "db1.prod.net"; let dbhost1 = "db1.prod.net";
let dbhost2 = "db2.prod.net"; let dbhost2 = "db2.prod.net";
let dbname = "testdb"; let dbname = "testdb";
// Constructor for database connection strings.
let mk_db_conn = macro (host, port, db) => { let mk_db_conn = macro (host, port, db) => {
host = host, host = host,
port = port, port = port,
@ -12,8 +14,10 @@ let mk_db_conn = macro (host, port, db) => {
let db_conn1 = mk_db_conn(dbhost1, 3306, dbname); let db_conn1 = mk_db_conn(dbhost1, 3306, dbname);
let db_conn2 = mk_db_conn(dbhost2, 3306, dbname); let db_conn2 = mk_db_conn(dbhost2, 3306, dbname);
// We have two database connections in a list
let db_conns = [db_conn1.conn_string, db_conn2.conn_string]; let db_conns = [db_conn1.conn_string, db_conn2.conn_string];
// Our server configuration.
let server_config = { let server_config = {
dbconn_list = db_conns, dbconn_list = db_conns,
db_conn1 = db_conns.0, db_conn1 = db_conns.0,

View File

@ -86,12 +86,14 @@ macro_rules! value_node {
}; };
} }
#[allow(unused_macros)]
macro_rules! make_tok { macro_rules! make_tok {
( $e: expr, $l:expr, $c:expr ) => { ( $e: expr, $l:expr, $c:expr ) => {
Token::new($e, $l, $c) Token::new($e, $l, $c)
}; };
} }
#[allow(unused_macros)]
macro_rules! make_expr { macro_rules! make_expr {
( $e:expr ) => { ( $e:expr ) => {
make_expr!($e, 1, 1) make_expr!($e, 1, 1)
@ -113,6 +115,7 @@ macro_rules! make_expr {
/// ///
/// make_selector!(foo", ["bar"] => 1, 0); /// make_selector!(foo", ["bar"] => 1, 0);
/// ``` /// ```
#[allow(unused_macros)]
macro_rules! make_selector { macro_rules! make_selector {
( $h:expr ) => { ( $h:expr ) => {
make_selector!($h, 1, 0) make_selector!($h, 1, 0)

View File

@ -11,6 +11,8 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//#![feature(trace_macros,log_syntax)]
#[macro_use] #[macro_use]
extern crate nom; extern crate nom;
#[macro_use] #[macro_use]

View File

@ -23,6 +23,14 @@ use ast::*;
use tokenizer::*; use tokenizer::*;
use error as E; use error as E;
macro_rules! eat_space {
($i:expr, $($args:tt)*) => (
{
sep!($i, emptyspace, $($args)*)
}
)
}
type ParseResult<O> = Result<O, Box<Error>>; type ParseResult<O> = Result<O, Box<Error>>;
fn symbol_to_value(s: Token) -> ParseResult<Value> { fn symbol_to_value(s: Token) -> ParseResult<Value> {
@ -116,7 +124,7 @@ named!(
field_value( Span ) -> (Token, Expression), field_value( Span ) -> (Token, Expression),
do_parse!( do_parse!(
field: barewordtok >> field: barewordtok >>
ws!(equaltok) >> eat_space!(equaltok) >>
value: expression >> value: expression >>
(field, value) (field, value)
) )
@ -128,7 +136,7 @@ fn vec_to_tuple(t: (Span, FieldList)) -> ParseResult<Value> {
} }
named!(field_list( Span ) -> FieldList, named!(field_list( Span ) -> FieldList,
separated_list!(commatok, ws!(field_value))); separated_list!(commatok, eat_space!(field_value)));
named!( named!(
#[doc="Capture a tuple of named fields with values. {<field>=<value>,...}"], #[doc="Capture a tuple of named fields with values. {<field>=<value>,...}"],
@ -137,7 +145,7 @@ named!(
do_parse!( do_parse!(
pos: position!() >> pos: position!() >>
v: delimited!(lbracetok, v: delimited!(lbracetok,
ws!(field_list), eat_space!(field_list),
rbracetok) >> rbracetok) >>
(pos, v) (pos, v)
), ),
@ -157,7 +165,7 @@ named!(list_value( Span ) -> Value,
do_parse!( do_parse!(
pos: position!() >> pos: position!() >>
leftsquarebracket >> leftsquarebracket >>
elements: ws!(separated_list!(ws!(commatok), expression)) >> elements: eat_space!(separated_list!(eat_space!(commatok), expression)) >>
rightsquarebracket >> rightsquarebracket >>
(pos, elements) (pos, elements)
), ),
@ -204,7 +212,7 @@ macro_rules! do_binary_expr {
$i, do_parse!( $i, do_parse!(
pos: position!() >> pos: position!() >>
left: value >> left: value >>
ws!($fn) >> eat_space!($fn) >>
right: expression >> right: expression >>
(pos, $typ, left, right) (pos, $typ, left, right)
), ),
@ -329,7 +337,7 @@ named!(copy_expression( Span ) -> Expression,
pos: position!() >> pos: position!() >>
selector: selector_list >> selector: selector_list >>
lbracetok >> lbracetok >>
fields: ws!(field_list) >> fields: eat_space!(field_list) >>
rbracetok >> rbracetok >>
(pos, SelectorDef::new(selector, pos.line as usize, pos.offset as usize), fields) (pos, SelectorDef::new(selector, pos.line as usize, pos.offset as usize), fields)
), ),
@ -366,17 +374,17 @@ fn tuple_to_macro(mut t: (Span, Vec<Value>, Value)) -> ParseResult<Expression> {
} }
} }
named!(arglist( Span ) -> Vec<Value>, separated_list!(ws!(commatok), symbol)); named!(arglist( Span ) -> Vec<Value>, separated_list!(eat_space!(commatok), symbol));
named!(macro_expression( Span ) -> Expression, named!(macro_expression( Span ) -> Expression,
map_res!( map_res!(
do_parse!( do_parse!(
pos: position!() >> pos: position!() >>
macrotok >> macrotok >>
ws!(lparentok) >> eat_space!(lparentok) >>
arglist: ws!(arglist) >> arglist: eat_space!(arglist) >>
rparentok >> rparentok >>
ws!(fatcommatok) >> eat_space!(fatcommatok) >>
map: tuple >> map: tuple >>
(pos, arglist, map) (pos, arglist, map)
), ),
@ -410,9 +418,9 @@ named!(select_expression( Span ) -> Expression,
do_parse!( do_parse!(
pos: position!() >> pos: position!() >>
selecttok >> selecttok >>
val: ws!(terminated!(expression, commatok)) >> val: eat_space!(terminated!(expression, commatok)) >>
default: ws!(terminated!(expression, commatok)) >> default: eat_space!(terminated!(expression, commatok)) >>
map: ws!(tuple) >> map: eat_space!(tuple) >>
(pos, val, default, map) (pos, val, default, map)
), ),
tuple_to_select tuple_to_select
@ -430,10 +438,10 @@ fn tuple_to_format(t: (Token, Vec<Expression>)) -> ParseResult<Expression> {
named!(format_expression( Span ) -> Expression, named!(format_expression( Span ) -> Expression,
map_res!( map_res!(
do_parse!( do_parse!(
tmpl: ws!(strtok) >> tmpl: eat_space!(strtok) >>
ws!(pcttok) >> eat_space!(pcttok) >>
lparentok >> lparentok >>
args: ws!(separated_list!(ws!(commatok), expression)) >> args: eat_space!(separated_list!(eat_space!(commatok), expression)) >>
rparentok >> rparentok >>
(tmpl, args) (tmpl, args)
), ),
@ -466,7 +474,7 @@ named!(selector_value( Span ) -> Value,
map_res!( map_res!(
do_parse!( do_parse!(
pos: position!() >> pos: position!() >>
sl: ws!(selector_list) >> sl: eat_space!(selector_list) >>
(pos, sl) (pos, sl)
), ),
vec_to_selector_value vec_to_selector_value
@ -479,7 +487,7 @@ named!(call_expression( Span ) -> Expression,
pos: position!() >> pos: position!() >>
macroname: selector_value >> macroname: selector_value >>
lparentok >> lparentok >>
args: ws!(separated_list!(ws!(commatok), expression)) >> args: eat_space!(separated_list!(eat_space!(commatok), expression)) >>
rparentok >> rparentok >>
(pos, macroname, args) (pos, macroname, args)
), ),
@ -509,7 +517,7 @@ named!(expression( Span ) -> Expression,
complete!(select_expression) | complete!(select_expression) |
complete!(call_expression) | complete!(call_expression) |
complete!(copy_expression) | complete!(copy_expression) |
ws!(simple_expression) eat_space!(simple_expression)
) )
); );
@ -519,7 +527,7 @@ fn expression_to_statement(v: Expression) -> ParseResult<Statement> {
named!(expression_statement( Span ) -> Statement, named!(expression_statement( Span ) -> Statement,
map_res!( map_res!(
terminated!(ws!(expression), semicolontok), terminated!(eat_space!(expression), semicolontok),
expression_to_statement expression_to_statement
) )
); );
@ -535,9 +543,9 @@ named!(let_statement( Span ) -> Statement,
map_res!( map_res!(
terminated!(do_parse!( terminated!(do_parse!(
lettok >> lettok >>
name: ws!(barewordtok) >> name: eat_space!(barewordtok) >>
equaltok >> equaltok >>
val: ws!(expression) >> val: eat_space!(expression) >>
(name, val) (name, val)
), semicolontok), ), semicolontok),
tuple_to_let tuple_to_let
@ -555,9 +563,9 @@ named!(import_statement( Span ) -> Statement,
map_res!( map_res!(
terminated!(do_parse!( terminated!(do_parse!(
importtok >> importtok >>
path: ws!(strtok) >> path: eat_space!(strtok) >>
astok >> astok >>
name: ws!(barewordtok) >> name: eat_space!(barewordtok) >>
(name, path) (name, path)
), semicolontok), ), semicolontok),
tuple_to_import tuple_to_import
@ -576,7 +584,7 @@ pub fn parse(input: Span) -> IResult<Span, Vec<Statement>> {
let mut out = Vec::new(); let mut out = Vec::new();
let mut i = input; let mut i = input;
loop { loop {
match ws!(i, statement) { match eat_space!(i, statement) {
IResult::Error(e) => { IResult::Error(e) => {
return IResult::Error(e); return IResult::Error(e);
} }

View File

@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use nom_locate::LocatedSpan; use nom_locate::LocatedSpan;
use nom::{alpha, is_alphanumeric, digit}; use nom;
use nom::{alpha, is_alphanumeric, digit, InputLength, sp};
use ast::*; use ast::*;
@ -145,6 +146,10 @@ named!(pub rightsquarebracket( Span ) -> Token,
do_tag_tok!("]") do_tag_tok!("]")
); );
named!(pub commentprefix( Span ) -> Token,
do_tag_tok!("//")
);
named!(pub fatcommatok( Span ) -> Token, named!(pub fatcommatok( Span ) -> Token,
do_tag_tok!("=>") do_tag_tok!("=>")
); );
@ -168,3 +173,39 @@ named!(pub importtok( Span ) -> Token,
named!(pub astok( Span ) -> Token, named!(pub astok( Span ) -> Token,
do_tag_tok!("as") do_tag_tok!("as")
); );
pub fn end_of_input(input: Span) -> nom::IResult<Span, Span> {
if input.input_len() == 0 {
return nom::IResult::Done(input, input);
} else {
return nom::IResult::Incomplete(nom::Needed::Unknown);
}
}
fn comment(input: Span) -> nom::IResult<Span, Span> {
match commentprefix(input) {
nom::IResult::Done(rest, _) => {
match alt!(rest, take_until!("\r\n") | take_until!("\n")) {
nom::IResult::Done(rest, cmt) => nom::IResult::Done(rest, cmt),
nom::IResult::Incomplete(i) => nom::IResult::Incomplete(i),
nom::IResult::Error(e) => {
if let nom::ErrorKind::Eof = e {
return nom::IResult::Done(input, input)
} else {
return nom::IResult::Error(e)
}
}
}
}
nom::IResult::Incomplete(i) => {
return nom::IResult::Incomplete(i)
}
nom::IResult::Error(e) => {
return nom::IResult::Error(e)
}
}
}
named!(pub emptyspace( Span ) -> Span,
alt!(sp | comment)
);