badicsalex / peginator

PEG parser generator for creating ASTs in Rust
MIT License
34 stars 3 forks source link

Cannot add @position to external rules #12

Closed oovm closed 1 year ago

oovm commented 1 year ago

peg side

@position
@extern(crate::utils::unicode_whitespace)
UnicodeSpace;

rust side

pub fn unicode_whitespace(input: &str) -> Result<(&str, usize), &'static str> {
    let mut length = 0;
    for char in input.chars() {
        if char.is_whitespace() { length += char.len_utf8() } else { break }
    }
    if length == 0 { Err("Not unicode whitespace") } else { Ok((&input[0..length], length)) }
}

except:

start offset + length offset

actual:

Caused by:
  process didn't exit successfully: `target\debug\build\saha-parser-579f728ace3fbfac\build-script-build` (exit code: 101)
  --- stderr
  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: expected character from character class IdentifierChar
  --> src/parser.peg:54:1
   |
   |  @position
   |  ^
  ', projects\saha-parser\build.rs:7:77
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
badicsalex commented 1 year ago

Without @position, UnicodeSpace will be a type alias to String. I guess in your example it would become a struct with a single value and position fields? Or just the position? I'm not sure how universal these would be and if it fit anyone else's use-case. I'd prefer to wait for someone else with a similar problem.

For now I'd suggest doing something like:

@extern(crate::utils::unicode_whitespace)
UnicodeSpaceExt;

@position
UnicodeSpace = spaces:UnicodeSpaceExt;
oovm commented 1 year ago

Good idea