Added dashes to identifiers

This commit is contained in:
Bruce Hill 2020-09-10 03:41:03 -07:00
parent aa0b8fc591
commit 4e201e6a70
3 changed files with 37 additions and 32 deletions

View File

@ -1,14 +1,14 @@
# This is a file defining the BPEG grammar using BPEG syntax
grammar;
grammar = __ @[mainPattern]extendedPat __ (*def % (__`;__)) ?(__ `;) __;
def = @[name]ref __ `= __ @[definition]extendedPat;
grammar = __ @[main-pattern]extended-pat __ (*def % (__`;__)) ?(__ `;) __;
def = @[name]ref __ `= __ @[definition]extended-pat;
# This is used for command line arguments:
stringGrammar = *(`\ pat ?`; / .);
string-grammar = *(`\ pat ?`; / .);
pat = empty / dot / string / charRange / char / escapeRange / escape / no / anythingBut
/ uptoAnd / repeat / after / before / capture / replace / ref / parens;
pat = empty / dot / string / char-range / char / escape-range / escape / no / anything-but
/ upto-and / repeat / after / before / capture / replace / ref / parens;
empty = `/ >(__ (`}/`}));
dot = `.;
@ -16,18 +16,18 @@ string = (
`" @[s]*(escape / ~`") `"
/ `' @[s]*(escape / ~`') `'
);
charRange = `` @[low]. `- @[high].;
char-range = `` @[low]. `- @[high].;
char = `` @[s].;
escapeRange = `\ @[low]escapeSequence `- @[high]escapeSequence;
escape = `\ @[s]escapeSequence;
escapeSequence = (
escape-range = `\ @[low]escape-sequence `- @[high]escape-sequence;
escape = `\ @[s]escape-sequence;
escape-sequence = (
1-3 `0-7
/ `x 2 (`0-9/`a-f/`A-F)
/`a/`b/`e/`n/`r/`t/`v / . / \n
);
no = `! _ @pat;
anythingBut = `~ ?`~ _ @pat;
uptoAnd = `& ?`& _ @pat;
anything-but = `~ ?`~ _ @pat;
upto-and = `& ?`& _ @pat;
repeat = (
@[min]int _ `- _ @[max]int
/ @[min]{->"0"} @[max]int _ `-
@ -36,26 +36,26 @@ repeat = (
/ `+ @[min]{->"1"} @[max](/)
/ `* @[min]{->"0"} @[max](/)
/ `? @[min]{->"0"} @[max]{->"1"}
) _ @[repeatPat]pat ?( __ `% __ @[sep]pat);
) _ @[repeat-pat]pat ?( __ `% __ @[sep]pat);
after = `< _ pat;
before = `> _ pat;
capture = `@ ?(_ `[ @[captureName]ref `]) _ @[capture]pat;
capture = `@ ?(_ `[ @[capture-name]ref `]) _ @[capture]pat;
replace = `{ __ (
?(@[replacePat]extendedPat __) "=>" ?(__ @[replacement]string)
?(@[replace-pat]extended-pat __) "=>" ?(__ @[replacement]string)
) __ `};
ref = @[name](
"^^" / "^" / "__" / "_" / "$$" / "$" /
(`a-z/`A-Z) *(`a-z/`A-Z/`0-9));
(`a-z/`A-Z) *(`a-z/`A-Z/`0-9/`-));
parens = `( __ extendedPat __ `);
parens = `( __ extended-pat __ `);
chain = +@pat % (__);
otherwise = +@(chain/pat) % (__`/__);
extendedPat = otherwise / chain / pat;
extended-pat = otherwise / chain / pat;
_ = *(` / \t);
__ = *(` / \t / \r / \n / comment);
hashComment = `# *.;
hash-comment = `# *.;
# Note: comments are undefined by default in regular BPEG
comment = hashComment;
comment = hash-comment;

21
bpeg.c
View File

@ -702,14 +702,10 @@ static vm_op_t *compile_bpeg(const char *source, const char *str)
visualize(source, str, "Ref");
--str;
const char *refname = str;
size_t len = 1;
for (++str; isalnum(*str); ++str) {
++len;
visualize(source, str, NULL);
}
str = after_name(str);
op->op = VM_REF;
//debug("Ref: %s\n", refname);
op->args.s = strndup(refname, len);
op->len = (size_t)(str - refname);
op->args.s = strndup(refname, op->len);
break;
} else {
visualize(source, str, "Finished");
@ -828,10 +824,10 @@ static void load_defs(void)
load_def("esc", "\\e"); load_def("e", "\\e");
load_def("tab", "\\t"); load_def("t", "\\t");
load_def("nl", "\\n"); load_def("lf", "\\n"); load_def("n", "\\n");
load_def("cBlockComment", "'/*' &&'*/'");
load_def("cLineComment", "'//' &$");
load_def("cComment", "cLineComment / cBlockComment");
load_def("hashComment", "`# &$");
load_def("c-block-comment", "'/*' &&'*/'");
load_def("c-line-comment", "'//' &$");
load_def("c-comment", "c-line-comment / c-block-comment");
load_def("hash-comment", "`# &$");
load_def("comment", "!(/)"); // undefined by default
load_def("WS", "` /\\t/\\n/\\r/comment");
load_def("ws", "` /\\t");
@ -1062,8 +1058,7 @@ static vm_op_t *load_grammar(const char *grammar)
name = strndup(name, 1);
defs += 1;
} else {
check(isalpha(*name), "Definition must begin with a name");
while (isalnum(*defs)) ++defs;
defs = after_name(defs);
name = strndup(name, (size_t)(defs-name));
}
defs = after_spaces(defs);

10
utils.h
View File

@ -31,6 +31,16 @@ static inline const char *after_spaces(const char *str)
return str;
}
static inline const char *after_name(const char *str)
{
if (!isalpha(*str)) return NULL;
for (++str; *str; ++str) {
if (!(isalnum(*str) || *str == '-'))
break;
}
return str;
}
static inline int matchchar(const char **str, char c)
{
*str = after_spaces(*str);