nodejs / llparse

Generating parsers in LLVM IR
http://llparse.org
Other
584 stars 30 forks source link

peek multiple characters? #55

Open cloewen8 opened 1 year ago

cloewen8 commented 1 year ago

My goal is to peek '\u{D3}' which is counted as 2 characters and perform a test, otherwise start a span.

I'm not very familiar with llparse and can't find any documentation on how to do this. I tried using select, but would then need to re-add the first character. How would I peek multiple characters or re-prepend the first character?

Here is what I had (based on llhttp):

n('header_field_start') // Match node
  .match('\r', n('headers_almost_done'))
  .peek('\n', n('headers_almost_done'))
  .peek(
    '\u{D3}', // 2 characters
    this.testLenientFlags(
      LENIENT_FLAGS.NTRIP, 
      {
        1: this.invokePausable('on_chunk_complete', ERROR.CB_CHUNK_COMPLETE, 'message_done') // If NTRIP is set
      },
      p.error(ERROR.INVALID_VERSION, 'Invalid major version') // Otherwise
    )
  )
  .otherwise(span.headerField.start(n('header_field')));

// Defined earlier...
private testLenientFlags(flag: LENIENT_FLAGS, map: { [key: number]: Node }, next?: string | Node): Node {
  const p = this.llparse;
  const res = p.invoke(p.code.test('lenient_flags', flag), map);
  if (next !== undefined) {
    res.otherwise(this.node(next));
  }
  return res;
}