Fix error position

This commit is contained in:
dalance 2020-12-24 17:54:09 +09:00
parent b26b56d624
commit 8bc0052dbc
6 changed files with 67 additions and 5 deletions

View File

@ -2,6 +2,7 @@
## [Unreleased](https://github.com/dalance/sv-parser/compare/v0.9.0...Unreleased) - ReleaseDate
* [Fixed] wrong error position
* [Fixed] escaped_ideitifier at macro name [#28](https://github.com/dalance/sv-parser/issues/28)
* [Fixed] begin_keywords "1364-2001-noconfig" failure [#28](https://github.com/dalance/sv-parser/issues/28)
* [Changed] update nom to 6.0.0

View File

@ -95,11 +95,21 @@ pub fn sv_parser(s: Span) -> IResult<Span, SourceText> {
source_text(s)
}
pub fn sv_parser_incomplete(s: Span) -> IResult<Span, SourceText> {
nom_packrat::init!();
source_text_incomplete(s)
}
pub fn lib_parser(s: Span) -> IResult<Span, LibraryText> {
nom_packrat::init!();
library_text(s)
}
pub fn lib_parser_incomplete(s: Span) -> IResult<Span, LibraryText> {
nom_packrat::init!();
library_text_incomplete(s)
}
pub fn pp_parser(s: Span) -> IResult<Span, PreprocessorText> {
nom_packrat::init!();
preprocessor_text(s)

View File

@ -5,6 +5,14 @@ use crate::*;
#[tracable_parser]
#[packrat_parser]
pub(crate) fn library_text(s: Span) -> IResult<Span, LibraryText> {
let (s, a) = many0(white_space)(s)?;
let (s, (b, _)) = many_till(library_description, eof)(s)?;
Ok((s, LibraryText { nodes: (a, b) }))
}
#[tracable_parser]
#[packrat_parser]
pub(crate) fn library_text_incomplete(s: Span) -> IResult<Span, LibraryText> {
let (s, a) = many0(white_space)(s)?;
let (s, b) = many0(library_description)(s)?;
Ok((s, LibraryText { nodes: (a, b) }))

View File

@ -5,6 +5,15 @@ use crate::*;
#[tracable_parser]
#[packrat_parser]
pub(crate) fn source_text(s: Span) -> IResult<Span, SourceText> {
let (s, a) = many0(white_space)(s)?;
let (s, b) = opt(timeunits_declaration)(s)?;
let (s, (c, _)) = many_till(description, eof)(s)?;
Ok((s, SourceText { nodes: (a, b, c) }))
}
#[tracable_parser]
#[packrat_parser]
pub(crate) fn source_text_incomplete(s: Span) -> IResult<Span, SourceText> {
let (s, a) = many0(white_space)(s)?;
let (s, b) = opt(timeunits_declaration)(s)?;
let (s, c) = many0(description)(s)?;

View File

@ -22,6 +22,26 @@ macro_rules! test {
};
}
macro_rules! error_test {
( $x:expr, $y:expr, $p:expr ) => {
nom_packrat::init!();
let info = SpanInfo::default();
#[cfg(feature = "trace")]
let info = info.set_tracable_info(
info.get_tracable_info()
//.fold("white_space")
.fold("number")
.fold("binary_operator")
.fold("unary_operator"),
);
let ret = all_consuming($x)(Span::new_extra($y, info));
match ret {
Err(Err::Error(e)) => assert_eq!(nom_greedyerror::error_position(&e), $p),
_ => (),
}
};
}
mod unit {
use super::*;
@ -15893,6 +15913,19 @@ mod spec {
}
}
mod error {
use super::*;
#[test]
fn test1() {
error_test!(
source_text,
r##"module A(); parameter A = 1 endmodule"##,
Some(12)
);
}
}
#[test]
fn debug() {
test!(

View File

@ -1,13 +1,14 @@
#![recursion_limit = "256"]
use nom::combinator::all_consuming;
use nom_greedyerror::error_position;
use std::collections::HashMap;
use std::fmt;
use std::hash::BuildHasher;
use std::path::{Path, PathBuf};
pub use sv_parser_error::Error;
use sv_parser_parser::{lib_parser, sv_parser, Span, SpanInfo};
use sv_parser_parser::{
lib_parser, lib_parser_incomplete, sv_parser, sv_parser_incomplete, Span, SpanInfo,
};
pub use sv_parser_pp::preprocess::{
preprocess, preprocess_str, Define, DefineText, Defines, PreprocessedText,
};
@ -110,9 +111,9 @@ pub fn parse_sv_pp(
) -> Result<(SyntaxTree, Defines), Error> {
let span = Span::new_extra(text.text(), SpanInfo::default());
let result = if allow_incomplete {
sv_parser(span)
sv_parser_incomplete(span)
} else {
all_consuming(sv_parser)(span)
sv_parser(span)
};
match result {
Ok((_, x)) => Ok((
@ -200,7 +201,7 @@ pub fn parse_lib_pp(
) -> Result<(SyntaxTree, Defines), Error> {
let span = Span::new_extra(text.text(), SpanInfo::default());
let result = if allow_incomplete {
all_consuming(lib_parser)(span)
lib_parser_incomplete(span)
} else {
lib_parser(span)
};