From 0c49659f6d956d2d09d5df0b7019dc8f44aa86ed Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sun, 11 Nov 2012 20:39:08 +0000 Subject: add AST construction of constants in interfaces --- src/webidl-ast.c | 4 ++++ src/webidl-ast.h | 7 +++++++ src/webidl-parser.y | 59 +++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/webidl-ast.c b/src/webidl-ast.c index a1f2276..bc4ab01 100644 --- a/src/webidl-ast.c +++ b/src/webidl-ast.c @@ -232,6 +232,7 @@ webidl_node_getint(struct webidl_node *node) switch(node->type) { case WEBIDL_NODE_TYPE_MODIFIER: case WEBIDL_NODE_TYPE_TYPE_BASE: + case WEBIDL_NODE_TYPE_LITERAL_INT: return node->r.number; default: @@ -318,6 +319,9 @@ static const char *webidl_node_type_to_str(enum webidl_node_type type) case WEBIDL_NODE_TYPE_CONST: return "Const"; + case WEBIDL_NODE_TYPE_LITERAL_INT: + return "Literal (int)"; + default: return "Unknown"; } diff --git a/src/webidl-ast.h b/src/webidl-ast.h index d891dec..5d7c615 100644 --- a/src/webidl-ast.h +++ b/src/webidl-ast.h @@ -22,14 +22,21 @@ enum webidl_node_type { WEBIDL_NODE_TYPE_INTERFACE, WEBIDL_NODE_TYPE_INTERFACE_INHERITANCE, WEBIDL_NODE_TYPE_INTERFACE_IMPLEMENTS, + WEBIDL_NODE_TYPE_ATTRIBUTE, WEBIDL_NODE_TYPE_OPERATION, WEBIDL_NODE_TYPE_CONST, + WEBIDL_NODE_TYPE_OPTIONAL_ARGUMENT, WEBIDL_NODE_TYPE_ARGUMENT, WEBIDL_NODE_TYPE_ELLIPSIS, WEBIDL_NODE_TYPE_TYPE, WEBIDL_NODE_TYPE_TYPE_BASE, + WEBIDL_NODE_TYPE_LITERAL_NULL, + WEBIDL_NODE_TYPE_LITERAL_INT, + WEBIDL_NODE_TYPE_LITERAL_BOOL, + WEBIDL_NODE_TYPE_LITERAL_FLOAT, + }; enum webidl_type { diff --git a/src/webidl-parser.y b/src/webidl-parser.y index f71c19c..683a1a2 100644 --- a/src/webidl-parser.y +++ b/src/webidl-parser.y @@ -15,6 +15,7 @@ #include #include #include +#include #include "webidl-ast.h" @@ -155,12 +156,17 @@ webidl_error(YYLTYPE *locp, struct webidl_node **winbind_ast, const char *str) %type SingleType %type UnionType %type NonAnyType +%type ConstType %type PrimitiveType %type UnrestrictedFloatType %type FloatType %type UnsignedIntegerType %type IntegerType +%type FloatLiteral +%type BooleanLiteral +%type ConstValue + %type ReadOnly %type OptionalLong %type Inherit @@ -513,13 +519,12 @@ Const: constant = webidl_node_new(WEBIDL_NODE_TYPE_IDENT, NULL, $3); /* add constant type */ - //constant = webidl_node_prepend(constant, $2); + constant = webidl_node_prepend(constant, $2); /* add constant value */ - //constant = webidl_node_prepend(constant, $5); + constant = webidl_node_prepend(constant, $5); $$ = webidl_node_new(WEBIDL_NODE_TYPE_CONST, NULL, constant); - } ; @@ -530,26 +535,62 @@ ConstValue: FloatLiteral | TOK_INT_LITERAL + { + $$ = webidl_node_new(WEBIDL_NODE_TYPE_LITERAL_INT, NULL, (void *)$1); + } | - TOK_NULL_LITERAL + TOK_NULL_LITERAL + { + $$ = webidl_node_new(WEBIDL_NODE_TYPE_LITERAL_NULL, NULL, NULL); + } ; /* [28] */ BooleanLiteral: TOK_TRUE + { + $$ = webidl_node_new(WEBIDL_NODE_TYPE_LITERAL_BOOL, NULL, (void *)true); + } | TOK_FALSE + { + $$ = webidl_node_new(WEBIDL_NODE_TYPE_LITERAL_BOOL, NULL, (void *)false); + } ; /* [29] */ FloatLiteral: TOK_FLOAT_LITERAL + { + float *value; + value = malloc(sizeof(float)); + *value = strtof($1, NULL); + $$ = webidl_node_new(WEBIDL_NODE_TYPE_LITERAL_FLOAT, NULL, value); + } | '-' TOK_INFINITY + { + float *value; + value = malloc(sizeof(float)); + *value = -INFINITY; + $$ = webidl_node_new(WEBIDL_NODE_TYPE_LITERAL_FLOAT, NULL, value); + } | TOK_INFINITY + { + float *value; + value = malloc(sizeof(float)); + *value = INFINITY; + $$ = webidl_node_new(WEBIDL_NODE_TYPE_LITERAL_FLOAT, NULL, value); + } | TOK_NAN + { + float *value; + value = malloc(sizeof(float)); + *value = NAN; + $$ = webidl_node_new(WEBIDL_NODE_TYPE_LITERAL_FLOAT, NULL, value); + } ; /* [30] */ @@ -1090,8 +1131,18 @@ NonAnyType: /* [63] */ ConstType: PrimitiveType Null + { + $$ = webidl_node_new(WEBIDL_NODE_TYPE_TYPE, NULL, $1); + } | TOK_IDENTIFIER Null + { + struct webidl_node *type; + type = webidl_node_new(WEBIDL_NODE_TYPE_TYPE_BASE, NULL, (void *)WEBIDL_TYPE_USER); + type = webidl_node_new(WEBIDL_NODE_TYPE_IDENT, type, $1); + $$ = webidl_node_new(WEBIDL_NODE_TYPE_TYPE, NULL, type); + } + ; /* [64] */ -- cgit v1.2.3