2012-04-23 00:02:42 +01:00
|
|
|
/* Parser combinators for binary formats.
|
|
|
|
|
* Copyright (C) 2012 Meredith L. Patterson, Dan "TQ" Hirsch
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation, version 2.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-04-23 19:39:44 +01:00
|
|
|
#ifndef HAMMER_HAMMER__H
|
|
|
|
|
#define HAMMER_HAMMER__H
|
2012-04-22 04:47:08 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
#include <stdint.h>
|
2012-05-12 21:20:38 +01:00
|
|
|
#include "allocator.h"
|
2012-05-17 13:22:56 +02:00
|
|
|
|
2012-04-23 19:39:44 +01:00
|
|
|
#define BYTE_BIG_ENDIAN 0x1
|
|
|
|
|
#define BIT_BIG_ENDIAN 0x2
|
2012-04-29 01:45:52 +01:00
|
|
|
#define BIT_LITTLE_ENDIAN 0x0
|
|
|
|
|
#define BYTE_LITTLE_ENDIAN 0x0
|
2012-04-23 19:39:44 +01:00
|
|
|
|
2012-05-11 23:38:45 +01:00
|
|
|
typedef int bool;
|
2012-05-17 13:22:56 +02:00
|
|
|
|
|
|
|
|
typedef struct parse_state parse_state_t;
|
2012-04-22 04:47:08 +01:00
|
|
|
|
2012-05-01 03:21:14 +01:00
|
|
|
typedef enum token_type {
|
|
|
|
|
TT_NONE,
|
|
|
|
|
TT_BYTES,
|
|
|
|
|
TT_SINT,
|
|
|
|
|
TT_UINT,
|
|
|
|
|
TT_SEQUENCE,
|
2012-05-18 12:35:40 +02:00
|
|
|
TT_USER = 64,
|
2012-05-12 21:53:54 +01:00
|
|
|
TT_ERR,
|
2012-05-01 03:21:14 +01:00
|
|
|
TT_MAX
|
|
|
|
|
} token_type_t;
|
|
|
|
|
|
2012-05-17 15:51:19 +02:00
|
|
|
typedef struct counted_array {
|
|
|
|
|
size_t capacity;
|
|
|
|
|
size_t used;
|
|
|
|
|
arena_t arena;
|
|
|
|
|
void **elements;
|
|
|
|
|
} counted_array_t;
|
|
|
|
|
|
2012-05-01 00:33:47 +01:00
|
|
|
typedef struct parsed_token {
|
2012-05-01 03:21:14 +01:00
|
|
|
token_type_t token_type;
|
|
|
|
|
union {
|
|
|
|
|
struct {
|
|
|
|
|
const uint8_t *token;
|
|
|
|
|
size_t len;
|
|
|
|
|
} bytes;
|
|
|
|
|
int64_t sint;
|
|
|
|
|
uint64_t uint;
|
2012-05-11 23:38:45 +01:00
|
|
|
double dbl;
|
|
|
|
|
float flt;
|
2012-05-17 15:51:19 +02:00
|
|
|
counted_array_t *seq; // a sequence of parsed_token_t's
|
2012-05-18 12:35:40 +02:00
|
|
|
void *user;
|
2012-05-01 03:21:14 +01:00
|
|
|
};
|
2012-05-17 15:47:14 +02:00
|
|
|
size_t index;
|
|
|
|
|
char bit_offset;
|
2012-05-01 00:33:47 +01:00
|
|
|
} parsed_token_t;
|
|
|
|
|
|
2012-05-18 12:49:40 +02:00
|
|
|
/**
|
|
|
|
|
* The result of a successful parse.
|
|
|
|
|
* If a parse fails, the parse result will be NULL.
|
|
|
|
|
* If a parse is successful but there's nothing there (i.e., if end_p
|
|
|
|
|
* succeeds) then there's a parse result but its ast is NULL.
|
2012-05-03 01:40:23 +01:00
|
|
|
*/
|
2012-04-22 23:40:25 +01:00
|
|
|
typedef struct parse_result {
|
2012-05-01 03:21:14 +01:00
|
|
|
const parsed_token_t *ast;
|
2012-05-12 21:53:03 +01:00
|
|
|
arena_t arena;
|
2012-04-22 23:40:25 +01:00
|
|
|
} parse_result_t;
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Type of an action to apply to an AST, used in the action() parser.
|
2012-05-18 12:49:40 +02:00
|
|
|
* It can be any (user-defined) function that takes a parse_result_t*
|
|
|
|
|
* and returns a parsed_token_t*. (This is so that the user doesn't
|
|
|
|
|
* have to worry about memory allocation; action() does that for you.)
|
|
|
|
|
* Note that the tagged union in parsed_token_t* supports user-defined
|
|
|
|
|
* types, so you can create your own token types (corresponding to,
|
|
|
|
|
* say, structs) and stuff values for them into the void* in the
|
|
|
|
|
* tagged union in parsed_token_t.
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-22 03:57:27 +02:00
|
|
|
typedef const parsed_token_t* (*action_t)(parse_result_t *p);
|
2012-05-12 15:49:46 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Type of a boolean attribute-checking function, used in the
|
|
|
|
|
* attr_bool() parser. It can be any (user-defined) function that takes
|
2012-05-18 12:49:40 +02:00
|
|
|
* a parse_result_t* and returns true or false.
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
|
|
|
|
typedef bool (*predicate_t)(parse_result_t *p);
|
2012-05-12 15:49:46 +01:00
|
|
|
|
2012-04-22 23:40:25 +01:00
|
|
|
typedef struct parser {
|
2012-05-01 00:33:47 +01:00
|
|
|
parse_result_t* (*fn)(void *env, parse_state_t *state);
|
|
|
|
|
void *env;
|
2012-04-22 23:40:25 +01:00
|
|
|
} parser_t;
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Top-level function to call a parser that has been built over some
|
|
|
|
|
* piece of input (of known size).
|
|
|
|
|
*/
|
2012-05-03 01:58:09 +01:00
|
|
|
parse_result_t* parse(const parser_t* parser, const uint8_t* input, size_t length);
|
2012-04-22 23:40:25 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given a string, returns a parser that parses that string value.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_BYTES
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-01 00:33:47 +01:00
|
|
|
const parser_t* token(const uint8_t *str, const size_t len);
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given a single character, returns a parser that parses that
|
|
|
|
|
* character.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_UINT
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-04-30 03:44:10 +01:00
|
|
|
const parser_t* ch(const uint8_t c);
|
2012-05-01 00:33:47 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given two single-character bounds, lower and upper, returns a parser
|
|
|
|
|
* that parses a single character within the range [lower, upper]
|
|
|
|
|
* (inclusive).
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_UINT
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-24 11:01:18 +02:00
|
|
|
const parser_t* ch_range(const uint8_t lower, const uint8_t upper);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Given an integer parser, p, and two integer bounds, lower and upper,
|
|
|
|
|
* returns a parser that parses an integral value within the range
|
|
|
|
|
* [lower, upper] (inclusive).
|
|
|
|
|
*/
|
|
|
|
|
const parser_t* int_range(const parser_t *p, const int64_t lower, const int64_t upper);
|
2012-05-01 00:33:47 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Returns a parser that parses the specified number of bits. sign ==
|
|
|
|
|
* true if signed, false if unsigned.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_SINT if sign == true, TT_UINT if sign == false
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-11 23:38:45 +01:00
|
|
|
const parser_t* bits(size_t len, bool sign);
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Returns a parser that parses a signed 8-byte integer value.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_SINT
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-11 15:14:30 +01:00
|
|
|
const parser_t* int64();
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Returns a parser that parses a signed 4-byte integer value.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_SINT
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-11 15:14:30 +01:00
|
|
|
const parser_t* int32();
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Returns a parser that parses a signed 2-byte integer value.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_SINT
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-11 15:14:30 +01:00
|
|
|
const parser_t* int16();
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Returns a parser that parses a signed 1-byte integer value.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_SINT
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-11 15:14:30 +01:00
|
|
|
const parser_t* int8();
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Returns a parser that parses an unsigned 8-byte integer value.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_UINT
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-11 15:14:30 +01:00
|
|
|
const parser_t* uint64();
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Returns a parser that parses an unsigned 4-byte integer value.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_UINT
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-11 15:14:30 +01:00
|
|
|
const parser_t* uint32();
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Returns a parser that parses an unsigned 2-byte integer value.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_UINT
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-11 15:14:30 +01:00
|
|
|
const parser_t* uint16();
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Returns a parser that parses an unsigned 1-byte integer value.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_UINT
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-11 15:14:30 +01:00
|
|
|
const parser_t* uint8();
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given another parser, p, returns a parser that skips any whitespace
|
|
|
|
|
* and then applies p.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: p's result type
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-04-30 03:44:10 +01:00
|
|
|
const parser_t* whitespace(const parser_t* p);
|
2012-05-01 00:33:47 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given another parser, p, and a function f, returns a parser that
|
|
|
|
|
* applies p, then applies f to everything in the AST of p's result.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: any
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-12 15:49:46 +01:00
|
|
|
const parser_t* action(const parser_t* p, const action_t a);
|
2012-05-01 00:33:47 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Parse a single character *NOT* in the given charset.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_UINT
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-11 23:42:21 +01:00
|
|
|
const parser_t* not_in(const uint8_t *charset, int length);
|
2012-05-01 00:33:47 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* A no-argument parser that succeeds if there is no more input to
|
|
|
|
|
* parse.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: None. The parse_result_t exists but its AST is NULL.
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-04-30 03:44:10 +01:00
|
|
|
const parser_t* end_p();
|
2012-05-01 00:33:47 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* This parser always fails.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: NULL. Always.
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-04-30 03:44:10 +01:00
|
|
|
const parser_t* nothing_p();
|
2012-05-01 00:45:46 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given a null-terminated list of parsers, apply each parser in order.
|
|
|
|
|
* The parse succeeds only if all parsers succeed.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_SEQUENCE
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-12 00:40:54 +01:00
|
|
|
const parser_t* sequence(const parser_t* p, ...) __attribute__((sentinel));
|
2012-05-01 03:59:49 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given an array of parsers, p_array, apply each parser in order. The
|
|
|
|
|
* first parser to succeed is the result; if no parsers succeed, the
|
|
|
|
|
* parse fails.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: The type of the first successful parser's result.
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-12 00:40:54 +01:00
|
|
|
const parser_t* choice(const parser_t* p, ...) __attribute__((sentinel));
|
2012-05-01 03:59:49 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given two parsers, p1 and p2, this parser succeeds in the following
|
|
|
|
|
* cases:
|
2012-05-03 02:31:22 +01:00
|
|
|
* - if p1 succeeds and p2 fails
|
2012-05-12 00:24:56 +01:00
|
|
|
* - if both succeed but p1's result is as long as or shorter than p2's
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: p1's result type.
|
2012-05-03 02:31:22 +01:00
|
|
|
*/
|
2012-04-30 03:44:10 +01:00
|
|
|
const parser_t* butnot(const parser_t* p1, const parser_t* p2);
|
2012-05-03 02:31:22 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given two parsers, p1 and p2, this parser succeeds in the following
|
|
|
|
|
* cases:
|
2012-05-03 02:31:22 +01:00
|
|
|
* - if p1 succeeds and p2 fails
|
|
|
|
|
* - if both succeed but p2's result is shorter than p1's
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: p1's result type.
|
2012-05-03 02:31:22 +01:00
|
|
|
*/
|
2012-04-30 03:44:10 +01:00
|
|
|
const parser_t* difference(const parser_t* p1, const parser_t* p2);
|
2012-05-03 02:31:22 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given two parsers, p1 and p2, this parser succeeds if *either* p1 or
|
|
|
|
|
* p2 succeed, but not if they both do.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: The type of the result of whichever parser succeeded.
|
2012-05-03 02:31:22 +01:00
|
|
|
*/
|
2012-04-30 03:44:10 +01:00
|
|
|
const parser_t* xor(const parser_t* p1, const parser_t* p2);
|
2012-05-03 02:31:22 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given a parser, p, this parser succeeds for zero or more repetitions
|
|
|
|
|
* of p.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_SEQUENCE
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-13 01:01:26 +01:00
|
|
|
const parser_t* many(const parser_t* p);
|
2012-05-11 23:38:45 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given a parser, p, this parser succeeds for one or more repetitions
|
|
|
|
|
* of p.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_SEQUENCE
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-13 01:01:26 +01:00
|
|
|
const parser_t* many1(const parser_t* p);
|
2012-05-11 23:38:45 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given a parser, p, this parser succeeds for exactly N repetitions
|
|
|
|
|
* of p.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_SEQUENCE
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-04-30 03:44:10 +01:00
|
|
|
const parser_t* repeat_n(const parser_t* p, const size_t n);
|
2012-05-11 23:38:45 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given a parser, p, this parser succeeds with the value p parsed or
|
|
|
|
|
* with an empty result.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: If p succeeded, the type of its result; if not, TT_NONE.
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-04-30 03:44:10 +01:00
|
|
|
const parser_t* optional(const parser_t* p);
|
2012-05-11 23:38:45 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given a parser, p, this parser succeeds if p succeeds, but doesn't
|
|
|
|
|
* include p's result in the result.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: None. The parse_result_t exists but its AST is NULL.
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-05-11 23:38:45 +01:00
|
|
|
const parser_t* ignore(const parser_t* p);
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given a parser, p, and a parser for a separator, sep, this parser
|
|
|
|
|
* matches a (possibly empty) list of things that p can parse,
|
|
|
|
|
* separated by sep.
|
|
|
|
|
* For example, if p is repeat1(range('0','9')) and sep is ch(','),
|
|
|
|
|
* sepBy(p, sep) will match a comma-separated list of integers.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_SEQUENCE
|
2012-05-12 15:49:46 +01:00
|
|
|
*/
|
2012-05-13 01:01:26 +01:00
|
|
|
const parser_t* sepBy(const parser_t* p, const parser_t* sep);
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* Given a parser, p, and a parser for a separator, sep, this parser matches a list of things that p can parse, separated by sep. Unlike sepBy, this ensures that the result has at least one element.
|
2012-05-13 01:01:26 +01:00
|
|
|
* For example, if p is repeat1(range('0','9')) and sep is ch(','), sepBy1(p, sep) will match a comma-separated list of integers.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: TT_SEQUENCE
|
2012-05-13 01:01:26 +01:00
|
|
|
*/
|
|
|
|
|
const parser_t* sepBy1(const parser_t* p, const parser_t* sep);
|
2012-05-12 15:49:46 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* This parser always returns a zero length match, i.e., empty string.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: None. The parse_result_t exists but its AST is NULL.
|
2012-05-18 12:18:19 +02:00
|
|
|
*/
|
2012-04-30 03:44:10 +01:00
|
|
|
const parser_t* epsilon_p();
|
2012-05-12 15:49:46 +01:00
|
|
|
|
2012-05-22 02:40:59 +02:00
|
|
|
/**
|
|
|
|
|
* This parser applies its first argument to read an unsigned integer
|
|
|
|
|
* value, then applies its second argument that many times. length
|
|
|
|
|
* should parse an unsigned integer value; this is checked at runtime.
|
|
|
|
|
* Specifically, the token_type of the returned token must be TT_UINT.
|
|
|
|
|
* In future we might relax this to include TT_USER but don't count on it.
|
|
|
|
|
*
|
|
|
|
|
* Result token type: TT_SEQUENCE
|
|
|
|
|
*/
|
|
|
|
|
const parser_t* length_value(const parser_t* length, const parser_t* value);
|
|
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
|
|
|
|
* This parser attaches a predicate function, which returns true or
|
|
|
|
|
* false, to a parser. The function is evaluated over the parser's
|
|
|
|
|
* result.
|
2012-05-24 11:01:18 +02:00
|
|
|
*
|
2012-05-12 15:49:46 +01:00
|
|
|
* The parse only succeeds if the attribute function returns true.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
2012-05-24 11:01:18 +02:00
|
|
|
* attr_bool will check whether p's result exists and whether p's
|
|
|
|
|
* result AST exists; you do not need to check for this in your
|
|
|
|
|
* predicate function.
|
|
|
|
|
*
|
2012-05-22 02:40:59 +02:00
|
|
|
* Result token type: p's result type if pred succeeded, NULL otherwise.
|
2012-05-12 15:49:46 +01:00
|
|
|
*/
|
2012-05-18 12:18:19 +02:00
|
|
|
const parser_t* attr_bool(const parser_t* p, predicate_t pred);
|
2012-05-12 15:49:46 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
2012-05-22 02:40:59 +02:00
|
|
|
* The 'and' parser asserts that a conditional syntax is satisfied,
|
|
|
|
|
* but doesn't consume that conditional syntax.
|
2012-05-12 15:49:46 +01:00
|
|
|
* This is useful for lookahead. As an example:
|
|
|
|
|
*
|
2012-05-18 12:18:19 +02:00
|
|
|
* Suppose you already have a parser, hex_p, that parses numbers in
|
|
|
|
|
* hexadecimal format (including the leading '0x'). Then
|
2012-05-12 15:49:46 +01:00
|
|
|
* sequence(and(token((const uint8_t*)"0x", 2)), hex_p)
|
2012-05-18 12:18:19 +02:00
|
|
|
* checks to see whether there is a leading "0x", *does not* consume
|
|
|
|
|
* the "0x", and then applies hex_p to parse the hex-formatted number.
|
2012-05-12 15:49:46 +01:00
|
|
|
*
|
2012-05-22 02:40:59 +02:00
|
|
|
* 'and' succeeds if p succeeds, and fails if p fails.
|
|
|
|
|
*
|
|
|
|
|
* Result token type: None. The parse_result_t exists but its AST is NULL.
|
2012-05-12 15:49:46 +01:00
|
|
|
*/
|
2012-04-30 03:44:10 +01:00
|
|
|
const parser_t* and(const parser_t* p);
|
2012-05-12 15:49:46 +01:00
|
|
|
|
2012-05-18 12:18:19 +02:00
|
|
|
/**
|
2012-05-22 02:40:59 +02:00
|
|
|
* The 'not' parser asserts that a conditional syntax is *not*
|
|
|
|
|
* satisfied, but doesn't consume that conditional syntax.
|
2012-05-12 15:49:46 +01:00
|
|
|
* As a somewhat contrived example:
|
|
|
|
|
*
|
|
|
|
|
* Since 'choice' applies its arguments in order, the following parser:
|
|
|
|
|
* sequence(ch('a'), choice(ch('+'), token((const uint8_t*)"++"), NULL), ch('b'), NULL)
|
2012-05-18 12:18:19 +02:00
|
|
|
* will not parse "a++b", because once choice() has succeeded, it will
|
|
|
|
|
* not backtrack and try other alternatives if a later parser in the
|
|
|
|
|
* sequence fails.
|
|
|
|
|
* Instead, you can force the use of the second alternative by turning
|
|
|
|
|
* the ch('+') alternative into a sequence with not:
|
2012-05-12 15:49:46 +01:00
|
|
|
* sequence(ch('a'), choice(sequence(ch('+'), not(ch('+')), NULL), token((const uint8_t*)"++")), ch('b'), NULL)
|
2012-05-18 12:18:19 +02:00
|
|
|
* If the input string is "a+b", the first alternative is applied; if
|
|
|
|
|
* the input string is "a++b", the second alternative is applied.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: None. The parse_result_t exists but its AST is NULL.
|
2012-05-12 15:49:46 +01:00
|
|
|
*/
|
2012-04-30 03:44:10 +01:00
|
|
|
const parser_t* not(const parser_t* p);
|
2012-04-22 15:30:49 +01:00
|
|
|
|
2012-05-17 18:27:59 +02:00
|
|
|
/**
|
2012-05-18 12:18:19 +02:00
|
|
|
* Create a parser that just calls out to another, as yet unknown,
|
|
|
|
|
* parser.
|
2012-05-17 18:27:59 +02:00
|
|
|
* Note that the inner parser gets bound later, with bind_indirect.
|
|
|
|
|
* This can be used to create recursive parsers.
|
2012-05-22 02:40:59 +02:00
|
|
|
*
|
|
|
|
|
* Result token type: the type of whatever parser is bound to it with
|
|
|
|
|
* bind_indirect().
|
2012-05-17 18:27:59 +02:00
|
|
|
*/
|
|
|
|
|
parser_t *indirect();
|
|
|
|
|
|
|
|
|
|
/**
|
2012-05-18 12:18:19 +02:00
|
|
|
* Set the inner parser of an indirect. See comments on indirect for
|
|
|
|
|
* details.
|
2012-05-17 18:27:59 +02:00
|
|
|
*/
|
|
|
|
|
void bind_indirect(parser_t* indirect, parser_t* inner);
|
|
|
|
|
|
2012-04-23 19:39:44 +01:00
|
|
|
#endif // #ifndef HAMMER_HAMMER__H
|