mirror of
https://github.com/janet-lang/janet
synced 2025-01-23 21:56:52 +00:00
Handle comments in parser.
This commit is contained in:
parent
40b52dbe70
commit
1e8c1bb74c
@ -179,6 +179,7 @@ struct GstParser {
|
|||||||
uint32_t count;
|
uint32_t count;
|
||||||
uint32_t cap;
|
uint32_t cap;
|
||||||
uint32_t index;
|
uint32_t index;
|
||||||
|
uint32_t flags;
|
||||||
enum {
|
enum {
|
||||||
GST_PARSER_PENDING = 0,
|
GST_PARSER_PENDING = 0,
|
||||||
GST_PARSER_FULL,
|
GST_PARSER_FULL,
|
||||||
|
24
parse.c
24
parse.c
@ -383,8 +383,27 @@ static int form_state(GstParser *p, uint8_t c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Handle a character */
|
/* Handle a character */
|
||||||
static int dispatch_char(GstParser *p, uint8_t c) {
|
static void dispatch_char(GstParser *p, uint8_t c) {
|
||||||
int done = 0;
|
int done = 0;
|
||||||
|
++p->index;
|
||||||
|
|
||||||
|
/* Handle comments */
|
||||||
|
if (p->flags & GST_PARSER_FLAG_INCOMMENT) {
|
||||||
|
if (c == '\n') {
|
||||||
|
p->flags = GST_PARSER_FLAG_EXPECTING_COMMENT;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
} else if (p->flags & GST_PARSER_FLAG_EXPECTING_COMMENT) {
|
||||||
|
if (c == '#') {
|
||||||
|
p->flags = GST_PARSER_FLAG_INCOMMENT;
|
||||||
|
return;
|
||||||
|
} else if (!is_whitespace(c)) {
|
||||||
|
p->flags = 0;
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while (!done && p->status == GST_PARSER_PENDING) {
|
while (!done && p->status == GST_PARSER_PENDING) {
|
||||||
GstParseState *top = parser_peek(p);
|
GstParseState *top = parser_peek(p);
|
||||||
switch (top->type) {
|
switch (top->type) {
|
||||||
@ -402,8 +421,6 @@ static int dispatch_char(GstParser *p, uint8_t c) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
++p->index;
|
|
||||||
return !done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse a C style string. The first value encountered when parsed is put
|
/* Parse a C style string. The first value encountered when parsed is put
|
||||||
@ -431,5 +448,6 @@ void gst_parser(GstParser *p, Gst *vm) {
|
|||||||
p->error = NULL;
|
p->error = NULL;
|
||||||
p->status = GST_PARSER_PENDING;
|
p->status = GST_PARSER_PENDING;
|
||||||
p->value.type = GST_NIL;
|
p->value.type = GST_NIL;
|
||||||
|
p->flags = GST_PARSER_FLAG_EXPECTING_COMMENT;
|
||||||
parser_push(p, PTYPE_ROOT, ' ');
|
parser_push(p, PTYPE_ROOT, ' ');
|
||||||
}
|
}
|
||||||
|
4
parse.h
4
parse.h
@ -3,6 +3,10 @@
|
|||||||
|
|
||||||
#include "datatypes.h"
|
#include "datatypes.h"
|
||||||
|
|
||||||
|
/* Some parser flags */
|
||||||
|
#define GST_PARSER_FLAG_INCOMMENT 1
|
||||||
|
#define GST_PARSER_FLAG_EXPECTING_COMMENT 2
|
||||||
|
|
||||||
/* Initialize a parser */
|
/* Initialize a parser */
|
||||||
void gst_parser(GstParser *p, Gst *vm);
|
void gst_parser(GstParser *p, Gst *vm);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user