Blame src/regex/tbnf_regex_scanfns.h

c25718
/**************************************************************/
c25718
/*  treebnf: a tree oriented bnf library                      */
c25718
/*  Copyright (C) 2024  SysDeer Technologies, LLC             */
c25718
/*  Released under GPLv2 and GPLv3; see COPYING.TREEBNF.      */
c25718
/**************************************************************/
c25718
c25718
#ifndef TBNF_REGEX_SCANFNS_H
c25718
#define TBNF_REGEX_SCANFNS_H
c25718
c25718
#include <treebnf/treebnf.h>
c25718
c25718
/* in-brace scanners */
c25718
static inline int tbnf_regex_scan_brace_rbrace(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '}')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_brace_digit(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] >= '0')
c25718
		if (sctx->tok_scan_mark[0] <= '9')
c25718
			return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_brace_comma(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == ',')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
c25718
/* in-bracket scanners */
c25718
static inline int tbnf_regex_scan_bracket_escaped_char(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '\\')
c25718
		if (&sctx->tok_scan_mark[1] < sctx->tok_scan_cap)
c25718
			if (sctx->tok_scan_mark[1])
c25718
				return 2;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_bracket_character_class(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	const char * ch = 0;
c25718
c25718
	if (&sctx->tok_scan_mark[4] >= sctx->tok_scan_cap)
c25718
		return -1;
c25718
c25718
	if (sctx->tok_scan_mark[0] == '[')
c25718
		if (sctx->tok_scan_mark[1] == ':')
c25718
			ch = &sctx->tok_scan_mark[2];
c25718
c25718
	if (!ch)
c25718
		return -1;
c25718
c25718
	for (; (*ch >= 'a') && (*ch <= 'z') && (&ch[2] < sctx->tok_scan_cap); )
c25718
		ch++;
c25718
c25718
	if ((*ch++ == ':') && (*ch++ == ']'))
c25718
			return (ch - sctx->tok_scan_mark);
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_bracket_collation_symbol(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (&sctx->tok_scan_mark[4] >= sctx->tok_scan_cap)
c25718
		return -1;
c25718
c25718
	if (sctx->tok_scan_mark[0] == '[')
c25718
		if (sctx->tok_scan_mark[1] == '.')
c25718
			if (sctx->tok_scan_mark[3] == '.')
c25718
				if (sctx->tok_scan_mark[4] == ']')
c25718
					if (sctx->tok_scan_mark[2])
c25718
						return 5;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_bracket_equivalence_class(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	const char * ch = 0;
c25718
c25718
	if (&sctx->tok_scan_mark[4] >= sctx->tok_scan_cap)
c25718
		return -1;
c25718
c25718
	if (sctx->tok_scan_mark[0] == '[')
c25718
		if (sctx->tok_scan_mark[1] == '=')
c25718
			ch = &sctx->tok_scan_mark[2];
c25718
c25718
	if (!ch)
c25718
		return -1;
c25718
c25718
	for (; (*ch >= 'a') && (*ch <= 'z') && (&ch[2] < sctx->tok_scan_cap); )
c25718
		ch++;
c25718
c25718
	if ((*ch++ == '=') && (*ch++ == ']'))
c25718
			return (ch - sctx->tok_scan_mark);
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_bracket_rbracket(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == ']')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_bracket_hyphen(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '-')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_bracket_error(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '[')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
c25718
c25718
/* bracket state entry scanners */
c25718
static inline int tbnf_regex_scan_lbracket_circumflex_rbracket(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '[')
c25718
		if (&sctx->tok_scan_mark[2] < sctx->tok_scan_cap)
c25718
			if (sctx->tok_scan_mark[1] == '^')
c25718
				if (sctx->tok_scan_mark[2] == ']')
c25718
					return 3;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_lbracket_circumflex(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '[')
c25718
		if (&sctx->tok_scan_mark[1] < sctx->tok_scan_cap)
c25718
			if (sctx->tok_scan_mark[1] == '^')
c25718
				return 2;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_lbracket_rbracket(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '[')
c25718
		if (&sctx->tok_scan_mark[1] < sctx->tok_scan_cap)
c25718
			if (sctx->tok_scan_mark[1] == ']')
c25718
				return 2;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_lbracket(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '[')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
c25718
/* common scanners */
c25718
static inline int tbnf_regex_scan_escaped_char(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '\\')
c25718
		if (&sctx->tok_scan_mark[1] < sctx->tok_scan_cap)
c25718
			if (sctx->tok_scan_mark[1])
c25718
				return 2;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_circumflex_asterisk(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '^')
c25718
		if (&sctx->tok_scan_mark[1] < sctx->tok_scan_cap)
c25718
			if (sctx->tok_scan_mark[1] == '*')
c25718
				return 2;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_circumflex(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '^')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_lbrace(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '{')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_lparen(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '(')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_rparen(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == ')')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_asterisk(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '*')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_period(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '.')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_dollar(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '$')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_vline(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '|')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_qmark(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '?')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_plus(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0] == '+')
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
static inline int tbnf_regex_scan_char(const struct tbnf_scan_ctx * sctx)
c25718
{
c25718
	if (sctx->tok_scan_mark[0])
c25718
		return 1;
c25718
c25718
	return -1;
c25718
}
c25718
c25718
#endif