Skip to content

Commit ba2baf7

Browse files
committed
Add a phase to parser and parseJson() function to Chaos.h to parse JSON strings
1 parent 168e771 commit ba2baf7

File tree

6 files changed

+31
-6
lines changed

6 files changed

+31
-6
lines changed

Chaos.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,8 @@ enum Role getRole(char *name) {
452452
void raiseError(char *msg) {
453453
throw_error(E_RAISED_FROM_AN_EXTENSION, msg, NULL, 0, 0);
454454
}
455+
456+
void parseJson(char *json) {
457+
injectCode(json, INIT_JSON_PARSE);
458+
phase = PROGRAM;
459+
}

Chaos.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ enum Type getDictType(char *name);
6767
enum ValueType getValueType(char *name);
6868
enum Role getRole(char *name);
6969
void raiseError(char *msg);
70+
void parseJson(char *json);
7071

7172
struct Kaos {
7273
int (*defineFunction)(char *name, enum Type type, char *params_name[], unsigned params_type[], unsigned short params_length);
@@ -122,9 +123,10 @@ struct Kaos {
122123
void (*finishComplex)(enum Type type);
123124
enum Type (*getListType)(char *name);
124125
enum Type (*getDictType)(char *name);
125-
enum ValueType (*getValueType) (char *name);
126-
enum Role (*getRole) (char *name);
127-
void (*raiseError) (char *msg);
126+
enum ValueType (*getValueType)(char *name);
127+
enum Role (*getRole)(char *name);
128+
void (*raiseError)(char *msg);
129+
void (*parseJson)(char *json);
128130
};
129131

130132
struct Kaos kaos;

chaos.l

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ switch (phase)
128128
phase = PROGRAM;
129129
return START_PROGRAM;
130130
break;
131+
case INIT_JSON_PARSE:
132+
phase = JSON_PARSE;
133+
return START_JSON_PARSE;
131134
default:
132135
break;
133136
}
@@ -196,7 +199,16 @@ switch (phase)
196199
<COMMENT>"*/" {BEGIN(INITIAL);}
197200
<COMMENT>.|"\n" {yylineno++;}
198201

199-
(\"|\')([^\\(\"|\')]|\\.)*(\"|\') {
202+
(\")([^\\(\")]|\\.)*(\") {
203+
recordToken(strdup(yytext), yyleng);
204+
if (isStreamOpen()) {
205+
yylval.sval = (char*)calloc(strlen(yytext)-1, sizeof(char));
206+
strncpy(yylval.sval, &yytext[1], strlen(yytext)-2);
207+
return T_STRING;
208+
}
209+
}
210+
211+
(\')([^\\(\')]|\\.)*(\') {
200212
recordToken(strdup(yytext), yyleng);
201213
if (isStreamOpen()) {
202214
yylval.sval = (char*)calloc(strlen(yytext)-1, sizeof(char));

chaos.y

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ char *program_code;
5656
unsigned long long lluval;
5757
}
5858

59-
%token START_PROGRAM START_PREPARSE
59+
%token START_PROGRAM START_PREPARSE START_JSON_PARSE
6060
%token<bval> T_TRUE T_FALSE
6161
%token<ival> T_INT T_TIMES_DO_INT
6262
%token<fval> T_FLOAT
@@ -96,6 +96,7 @@ char *program_code;
9696
meta_start:
9797
| START_PROGRAM parser { }
9898
| START_PREPARSE preparser { }
99+
| START_JSON_PARSE json_parser { }
99100
;
100101

101102
preparser:
@@ -791,6 +792,10 @@ quit: { }
791792
}
792793
;
793794

795+
json_parser:
796+
| dictionarystart { Symbol* symbol = finishComplexMode(NULL, K_ANY); returnVariable(symbol); }
797+
;
798+
794799
%%
795800

796801
int main(int argc, char** argv) {

enums.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef ENUMS_H
22
#define ENUMS_H
33

4-
enum Phase { INIT_PREPARSE, PREPARSE, INIT_PROGRAM, PROGRAM };
4+
enum Phase { INIT_PREPARSE, PREPARSE, INIT_PROGRAM, PROGRAM, INIT_JSON_PARSE, JSON_PARSE };
55

66
enum Type { K_BOOL, K_NUMBER, K_STRING, K_ANY, K_LIST, K_DICT, K_VOID };
77
enum ValueType { V_BOOL, V_INT, V_FLOAT, V_STRING, V_VOID };

modules/extension.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void initKaosApi() {
5757
kaos.getValueType = getValueType;
5858
kaos.getRole = getRole;
5959
kaos.raiseError = raiseError;
60+
kaos.parseJson = parseJson;
6061
}
6162

6263
void callRegisterInDynamicLibrary(char* dynamic_library_path) {

0 commit comments

Comments
 (0)