Przeglądaj źródła

ncd: Add parser support for function invocations and parentheses in value expressions.

Ambroz Bizjak 12 lat temu
rodzic
commit
298e79776f

+ 18 - 0
examples/ncd_parser_test.c

@@ -147,6 +147,24 @@ static int generate_val (NCDValue *value, ExpString *out_str)
             }
             }
         } break;
         } break;
         
         
+        case NCDVALUE_INVOC: {
+            if (!generate_val(NCDValue_InvocFunc(value), out_str)) {
+                goto fail;
+            }
+            
+            if (!ExpString_AppendChar(out_str, '(')) {
+                goto fail;
+            }
+            
+            if (!generate_val(NCDValue_InvocArg(value), out_str)) {
+                goto fail;
+            }
+            
+            if (!ExpString_AppendChar(out_str, ')')) {
+                goto fail;
+            }
+        } break;
+        
         default: ASSERT(0);
         default: ASSERT(0);
     }
     }
     
     

Plik diff jest za duży
+ 434 - 373
generated/NCDConfigParser_parse.c


Plik diff jest za duży
+ 655 - 384
generated/NCDConfigParser_parse.out


+ 41 - 4
generated/NCDConfigParser_parse.y

@@ -100,6 +100,8 @@ static void free_value (struct value o) { if (o.have) NCDValue_Free(&o.v); }
 %type list { struct value }
 %type list { struct value }
 %type map_contents { struct value }
 %type map_contents { struct value }
 %type map  { struct value }
 %type map  { struct value }
+%type invoc { struct value }
+%type noninvoc_value  { struct value }
 %type value  { struct value }
 %type value  { struct value }
 %type name_maybe { char * }
 %type name_maybe { char * }
 %type process_or_template { int }
 %type process_or_template { int }
@@ -117,6 +119,8 @@ static void free_value (struct value o) { if (o.have) NCDValue_Free(&o.v); }
 %destructor list { free_value($$); }
 %destructor list { free_value($$); }
 %destructor map_contents { free_value($$); }
 %destructor map_contents { free_value($$); }
 %destructor map { free_value($$); }
 %destructor map { free_value($$); }
+%destructor invoc { free_value($$); }
+%destructor noninvoc_value { free_value($$); }
 %destructor value { free_value($$); }
 %destructor value { free_value($$); }
 %destructor name_maybe { free($$); }
 %destructor name_maybe { free($$); }
 
 
@@ -655,7 +659,28 @@ map(R) ::= BRACKET_OPEN map_contents(A) BRACKET_CLOSE. {
     R = A;
     R = A;
 }
 }
 
 
-value(R) ::= STRING(A). {
+invoc(R) ::= value(F) noninvoc_value(A). {
+    if (!F.have || !A.have) {
+        goto failQ0;
+    }
+    
+    if (!NCDValue_InitInvoc(&R.v, F.v, A.v)) {
+        goto failQ0;
+    }
+    F.have = 0;
+    A.have = 0;
+    R.have = 1;
+    goto doneQ;
+    
+failQ0:
+    R.have = 0;
+    parser_out->out_of_memory = 1;
+doneQ:
+    free_value(F);
+    free_value(A);
+}
+
+noninvoc_value(R) ::= STRING(A). {
     ASSERT(A.str)
     ASSERT(A.str)
 
 
     if (!NCDValue_InitStringBin(&R.v, (uint8_t *)A.str, A.len)) {
     if (!NCDValue_InitStringBin(&R.v, (uint8_t *)A.str, A.len)) {
@@ -672,7 +697,7 @@ doneU:
     free_token(A);
     free_token(A);
 }
 }
 
 
-value(R) ::= dotted_name(A). {
+noninvoc_value(R) ::= dotted_name(A). {
     if (!A) {
     if (!A) {
         goto failV0;
         goto failV0;
     }
     }
@@ -691,11 +716,23 @@ doneV:
     free(A);
     free(A);
 }
 }
 
 
-value(R) ::= list(A). {
+noninvoc_value(R) ::= list(A). {
+    R = A;
+}
+
+noninvoc_value(R) ::= map(A). {
+    R = A;
+}
+
+noninvoc_value(R) ::= ROUND_OPEN value(A) ROUND_CLOSE. {
+    R = A;
+}
+
+value(R) ::= invoc(A). {
     R = A;
     R = A;
 }
 }
 
 
-value(R) ::= map(A). {
+value(R) ::= noninvoc_value(A). {
     R = A;
     R = A;
 }
 }
 
 

+ 49 - 0
ncd/NCDAst.c

@@ -69,6 +69,7 @@ static void value_assert (NCDValue *o)
         case NCDVALUE_LIST:
         case NCDVALUE_LIST:
         case NCDVALUE_MAP:
         case NCDVALUE_MAP:
         case NCDVALUE_VAR:
         case NCDVALUE_VAR:
+        case NCDVALUE_INVOC:
             return;
             return;
         default:
         default:
             ASSERT(0);
             ASSERT(0);
@@ -109,6 +110,13 @@ void NCDValue_Free (NCDValue *o)
             free(o->var_name);
             free(o->var_name);
         } break;
         } break;
         
         
+        case NCDVALUE_INVOC: {
+            NCDValue_Free(o->invoc_arg);
+            NCDValue_Free(o->invoc_func);
+            free(o->invoc_arg);
+            free(o->invoc_func);
+        } break;
+        
         default:
         default:
             ASSERT(0);
             ASSERT(0);
     }
     }
@@ -368,6 +376,47 @@ const char * NCDValue_VarName (NCDValue *o)
     return o->var_name;
     return o->var_name;
 }
 }
 
 
+int NCDValue_InitInvoc (NCDValue *o, NCDValue func, NCDValue arg)
+{
+    value_assert(&func);
+    value_assert(&arg);
+    
+    if (!(o->invoc_func = malloc(sizeof(*o->invoc_func)))) {
+        goto fail0;
+    }
+    if (!(o->invoc_arg = malloc(sizeof(*o->invoc_arg)))) {
+        goto fail1;
+    }
+    
+    *o->invoc_func = func;
+    *o->invoc_arg = arg;
+    
+    o->type = NCDVALUE_INVOC;
+    
+    return 1;
+    
+fail1:
+    free(o->invoc_func);
+fail0:
+    return 0;
+}
+
+NCDValue * NCDValue_InvocFunc (NCDValue *o)
+{
+    value_assert(o);
+    ASSERT(o->type == NCDVALUE_INVOC)
+    
+    return o->invoc_func;
+}
+
+NCDValue * NCDValue_InvocArg (NCDValue *o)
+{
+    value_assert(o);
+    ASSERT(o->type == NCDVALUE_INVOC)
+    
+    return o->invoc_arg;
+}
+
 void NCDProgram_Init (NCDProgram *o)
 void NCDProgram_Init (NCDProgram *o)
 {
 {
     LinkedList1_Init(&o->elems_list);
     LinkedList1_Init(&o->elems_list);

+ 8 - 0
ncd/NCDAst.h

@@ -63,6 +63,10 @@ struct NCDValue_s {
         struct {
         struct {
             char *var_name;
             char *var_name;
         };
         };
+        struct {
+            NCDValue *invoc_func;
+            NCDValue *invoc_arg;
+        };
     };
     };
 };
 };
 
 
@@ -136,6 +140,7 @@ struct NCDIf_s {
 #define NCDVALUE_LIST 2
 #define NCDVALUE_LIST 2
 #define NCDVALUE_MAP 3
 #define NCDVALUE_MAP 3
 #define NCDVALUE_VAR 4
 #define NCDVALUE_VAR 4
+#define NCDVALUE_INVOC 5
 
 
 #define NCDPROGRAMELEM_PROCESS 1
 #define NCDPROGRAMELEM_PROCESS 1
 #define NCDPROGRAMELEM_INCLUDE 2
 #define NCDPROGRAMELEM_INCLUDE 2
@@ -165,6 +170,9 @@ NCDValue * NCDValue_MapNextKey (NCDValue *o, NCDValue *ekey);
 NCDValue * NCDValue_MapKeyValue (NCDValue *o, NCDValue *ekey);
 NCDValue * NCDValue_MapKeyValue (NCDValue *o, NCDValue *ekey);
 int NCDValue_InitVar (NCDValue *o, const char *var_name) WARN_UNUSED;
 int NCDValue_InitVar (NCDValue *o, const char *var_name) WARN_UNUSED;
 const char * NCDValue_VarName (NCDValue *o);
 const char * NCDValue_VarName (NCDValue *o);
+int NCDValue_InitInvoc (NCDValue *o, NCDValue func, NCDValue arg) WARN_UNUSED;
+NCDValue * NCDValue_InvocFunc (NCDValue *o);
+NCDValue * NCDValue_InvocArg (NCDValue *o);
 
 
 void NCDProgram_Init (NCDProgram *o);
 void NCDProgram_Init (NCDProgram *o);
 void NCDProgram_Free (NCDProgram *o);
 void NCDProgram_Free (NCDProgram *o);

+ 41 - 4
ncd/NCDConfigParser_parse.y

@@ -100,6 +100,8 @@ static void free_value (struct value o) { if (o.have) NCDValue_Free(&o.v); }
 %type list { struct value }
 %type list { struct value }
 %type map_contents { struct value }
 %type map_contents { struct value }
 %type map  { struct value }
 %type map  { struct value }
+%type invoc { struct value }
+%type noninvoc_value  { struct value }
 %type value  { struct value }
 %type value  { struct value }
 %type name_maybe { char * }
 %type name_maybe { char * }
 %type process_or_template { int }
 %type process_or_template { int }
@@ -117,6 +119,8 @@ static void free_value (struct value o) { if (o.have) NCDValue_Free(&o.v); }
 %destructor list { free_value($$); }
 %destructor list { free_value($$); }
 %destructor map_contents { free_value($$); }
 %destructor map_contents { free_value($$); }
 %destructor map { free_value($$); }
 %destructor map { free_value($$); }
+%destructor invoc { free_value($$); }
+%destructor noninvoc_value { free_value($$); }
 %destructor value { free_value($$); }
 %destructor value { free_value($$); }
 %destructor name_maybe { free($$); }
 %destructor name_maybe { free($$); }
 
 
@@ -655,7 +659,28 @@ map(R) ::= BRACKET_OPEN map_contents(A) BRACKET_CLOSE. {
     R = A;
     R = A;
 }
 }
 
 
-value(R) ::= STRING(A). {
+invoc(R) ::= value(F) noninvoc_value(A). {
+    if (!F.have || !A.have) {
+        goto failQ0;
+    }
+    
+    if (!NCDValue_InitInvoc(&R.v, F.v, A.v)) {
+        goto failQ0;
+    }
+    F.have = 0;
+    A.have = 0;
+    R.have = 1;
+    goto doneQ;
+    
+failQ0:
+    R.have = 0;
+    parser_out->out_of_memory = 1;
+doneQ:
+    free_value(F);
+    free_value(A);
+}
+
+noninvoc_value(R) ::= STRING(A). {
     ASSERT(A.str)
     ASSERT(A.str)
 
 
     if (!NCDValue_InitStringBin(&R.v, (uint8_t *)A.str, A.len)) {
     if (!NCDValue_InitStringBin(&R.v, (uint8_t *)A.str, A.len)) {
@@ -672,7 +697,7 @@ doneU:
     free_token(A);
     free_token(A);
 }
 }
 
 
-value(R) ::= dotted_name(A). {
+noninvoc_value(R) ::= dotted_name(A). {
     if (!A) {
     if (!A) {
         goto failV0;
         goto failV0;
     }
     }
@@ -691,11 +716,23 @@ doneV:
     free(A);
     free(A);
 }
 }
 
 
-value(R) ::= list(A). {
+noninvoc_value(R) ::= list(A). {
+    R = A;
+}
+
+noninvoc_value(R) ::= map(A). {
+    R = A;
+}
+
+noninvoc_value(R) ::= ROUND_OPEN value(A) ROUND_CLOSE. {
+    R = A;
+}
+
+value(R) ::= invoc(A). {
     R = A;
     R = A;
 }
 }
 
 
-value(R) ::= map(A). {
+value(R) ::= noninvoc_value(A). {
     R = A;
     R = A;
 }
 }
 
 

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików