with complete bracketing and indenting.
# File: indent_test.mk
- myCFLAGS := -Wall -g -fplan9-extensions
- CFLAGS := $(filter-out $(myCFLAGS),$(CFLAGS)) $(myCFLAGS)
- myLDLIBS:= libparser.o libscanner.o libmdcode.o -licuuc
- LDLIBS := $(filter-out $(myLDLIBS),$(LDLIBS)) $(myLDLIBS)
+ itestCFLAGS := -Wall -g -fplan9-extensions
+ itestLDLIBS:= libparser.o libscanner.o libmdcode.o -licuuc
all :: itest
- itest.c itest.h : indent_test.mdc parsergen libparser.o libscanner.o libmdcode.o
+ itest.c itest.h : indent_test.mdc parsergen
./parsergen -o itest --LALR --tag indent indent_test.mdc
indent_test.mk: indent_test.mdc md2c
./md2c indent_test.mdc
- itest: itest.c
+ itest: itest.c | $(filter %.o,$(itestLDLIBS))
+ $(CC) $(itestCFLAGS) $^ $(itestLDLIBS) -o $@
doitest: itest itest.code
./itest itest.code
checkitest: itest itest.code
@grep -v '^#' itest.out > .tmp.out
@./itest itest.code | diff -u - .tmp.out || echo itest FAILED
+ @sed -e 'i\
+ ' itest.code > itest2.code
+ @./itest itest2.code| diff -u - .tmp.out || echo itest2 FAILED
demos :: doitest
tests :: checkitest
char *file = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
struct section *s = code_extract(file, file+len, NULL);
struct token_config config = {
- .ignored = (1 << TK_line_comment)
- | (1 << TK_block_comment),
.number_chars = ".,_+-",
.word_start = "",
.word_cont = "",
~~~~~~
-Program -> Statementlist ${ print_statement($1, 0); }$
+$TERM if { } : * + - / ; =
-OptNL -> NEWLINE
- |
+Program -> Statementlist ${ print_statement($1, 0); }$
$*statement
+ Newlines -> NEWLINE
+ | Newlines NEWLINE
Statementlist -> Statements ${ $0 = $<1; }$
+ | Newlines Statements ${ $0 = $<2; }$
Statements -> Statements Statement ${
{
| ERROR ${ printf("statement ERROR\n"); $0 = NULL; }$
Open -> {
- | NEWLINE {
+ | Newlines {
Close -> }
- | NEWLINE }
+ | Newlines }
Block -> Open Statementlist Close ${ $0 = $<2; }$
| Open SimpleStatements } ${ $0 = $<2; }$
- | : Statementlist ${ $0 = $<2; }$
+ | : SimpleStatements ${ $0 = $<2; }$
+ | : StatementBlock ${ $0 = $<2; }$
+ StatementBlock -> Statementlist $$OUT ${ $0 = $<1; }$
SimpleStatements -> SimpleStatements ; SimpleStatement ${
{
$0->expr->op = $2.txt;
$0->expr->right = $<3;
}$
- Statement -> SimpleStatements NEWLINE ${
- $0 = $<1;
- }$
- | IfStatement ${ $0 = $<1; }$
+ SSline -> SimpleStatements NEWLINE ${ $0 = $<1; }$
+ Statement -> SSline ${ $0 = $<1; }$
+ | IfStatement $$NEWLINE ${ $0 = $<1; }$
| Statement NEWLINE ${ $0 = $<1; }$
- IfStatement -> if Expression Block OptNL ${
- $0 = calloc(1, sizeof(struct statement));
- $0->expr = $<2;
- $0->thenpart = $<3;
- }$
- | if Expression : SimpleStatements ${
- $0 = calloc(1, sizeof(struct statement));
- $0->expr = $<2;
- $0->thenpart = $<4;
- }$
- | if Expression Block OptNL else Block ${
- $0 = calloc(1, sizeof(struct statement));
- $0->expr = $<2;
- $0->thenpart = $<3;
- $0->elsepart = $<6;
- }$
- | if Expression Block OptNL else : SimpleStatements ${
- $0 = calloc(1, sizeof(struct statement));
- $0->expr = $<2;
- $0->thenpart = $<3;
- $0->elsepart = $<7;
- }$
- | if Expression Block OptNL else IfStatement ${
+ $RIGHT else
+
+ IfHead -> if Expression Block ${
$0 = calloc(1, sizeof(struct statement));
$0->expr = $<2;
$0->thenpart = $<3;
- $0->elsepart = $<6;
}$
+ | IfHead NEWLINE ${ $0 = $<1; }$
+
+ IfStatement -> IfHead $$else ${ $0 = $<1; }$
+ | IfHead else Block ${
+ $0 = $<1;
+ $0->elsepart = $<3;
+ }$
+ | IfHead else IfStatement ${
+ $0 = $<1;
+ $0->elsepart = $<3;
+ }$
$*expression
Expression -> Expression + Term ${
all = y;
if true {yes=x;} else : no=x
if true: yes = no; no = yes;
+ if false: yes=ok; else: no=ok
if false {
print = OK
if true:
(yes=no);
(no=yes);
+ if false:
+ (yes=ok);
+ else:
+ (no=ok);
if false:
(print=OK);
else: