aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mate/mate_parser.l
diff options
context:
space:
mode:
authorLuis Ontanon <luis.ontanon@gmail.com>2005-05-16 13:28:35 +0000
committerLuis Ontanon <luis.ontanon@gmail.com>2005-05-16 13:28:35 +0000
commit047baf2c3861ff98b694d7f9ca9db586d0c2f5b5 (patch)
treefe523c20e67ff9373d27191240b9ff29a1766f51 /plugins/mate/mate_parser.l
parenta14646212b4b7790da17d1f24b912484f30e0ac5 (diff)
MATE has a grammar.
Although not yet fully implemented I want this version as a reference. svn path=/trunk/; revision=14373
Diffstat (limited to 'plugins/mate/mate_parser.l')
-rw-r--r--plugins/mate/mate_parser.l296
1 files changed, 296 insertions, 0 deletions
diff --git a/plugins/mate/mate_parser.l b/plugins/mate/mate_parser.l
new file mode 100644
index 0000000000..c7a5001397
--- /dev/null
+++ b/plugins/mate/mate_parser.l
@@ -0,0 +1,296 @@
+%option noyywrap
+%option nounput
+%option never-interactive
+%option prefix="Mate"
+
+%{
+
+ /* mate_parser.l
+ * lexical analyzer for MATE configuration files
+ *
+ * Copyright 2004, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
+ *
+ * $Id: packet-xml.c 13427 2005-02-18 22:33:16Z lego $
+ *
+ * Ethereal - Network traffic analyzer
+ * By Gerald Combs <gerald@ethereal.com>
+ * Copyright 1998 Gerald Combs
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include "mate.h"
+#include "mate_grammar.h"
+
+ void MateParser(void*,int, gchar*, mate_config* matecfg);
+ void *MateParserAlloc(void *(*)(gulong));
+ void MateParserFree( void*, void(*)(void*) );
+ void MateParseTrace(FILE*,char*);
+
+#define MAX_INCLUDE_DEPTH 10
+ YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
+ int include_stack_ptr = 0;
+
+ void* pParser;
+ mate_config_frame* current_frame;
+
+ mate_config* mc;
+
+#define MATE_PARSE(token_type) MateParser(pParser, (token_type), g_strdup(yytext), mc );
+
+%}
+
+pdu_kw Pdu
+gop_kw Gop
+gog_kw Gog
+transform_kw Transform
+match_kw Match
+always_kw Always
+strict_kw Strict
+every_kw Every
+loose_kw Loose
+replace_kw Replace
+insert_kw Insert
+gop_tree_kw GopTree
+member_kw Member
+on_kw On
+start_kw Start
+stop_kw Stop
+extra_kw Extra
+show_tree_kw ShowTree
+show_times_kw ShowTimes
+expiration_kw Expiration
+idle_timeout_kw IdleTimeout
+lifetime_kw Lifetime
+no_tree_kw NoTree
+pdu_tree_kw PduTree
+frame_tree_kw FrameTree
+basic_tree_kw BasicTree
+true_kw [Tt][Rr][Uu][Ee]
+false_kw [Ff][Aa][Ll][Ss][Ee]
+proto_kw Proto
+payload_kw Payload
+transport_kw Transport
+criteria_kw Criteria
+accept_kw Accept
+reject_kw Reject
+extract_kw Extract
+from_kw From
+drop_unassigned_kw DropUnassigned
+discard_pdu_data_kw DiscardPduData
+last_pdu_kw LastPdu
+done_kw Done
+
+
+
+open_parens "("
+close_parens ")"
+open_brace "{"
+close_brace "}"
+comma ","
+semicolon ";"
+slash "/"
+pipe "|"
+
+integer [0-9]+
+floating ([0-9]+\.[0-9]+)
+doted_ip [0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?\.[0-9][0-9]?[0-9]?
+colonized [0-9A-Fa-f:]*[:][0-9A-Fa-f:]*
+
+name [a-z][-\.a-zA-Z0-9_]*
+avp_operator [$^~=<>!]
+quote ["]
+not_quoted [^"]*
+
+include "#include"
+filename [-A-Za-z0-9_/.]+
+
+whitespace [[:blank:]\r]+
+newline \n
+
+comment "//"[^\n]*\n
+
+blk_cmnt_start "/*"
+cmnt_char .
+blk_cmnt_stop "*/"
+
+%START OUT QUOTED INCLUDING COMMENT
+%%
+
+{newline} current_frame->linenum++;
+{whitespace} ;
+
+<OUT>{include} BEGIN INCLUDING;
+
+<INCLUDING>{filename} {
+ if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
+ g_error("dtd_preparse: include files nested to deeply");
+
+ include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
+ yyin = fopen( yytext, "r" );
+
+ if (!yyin) {
+ yy_delete_buffer( YY_CURRENT_BUFFER );
+
+ yy_switch_to_buffer(include_stack[--include_stack_ptr] );
+
+ if (errno)
+ g_string_sprintfa(mc->config_error, "Mate parser: Could not open file: '%s': %s", yytext, strerror(errno) );
+
+ } else {
+
+ current_frame = g_malloc(sizeof(mate_config_frame));
+ current_frame->filename = g_strdup(yytext);
+ current_frame->linenum = 1;
+
+ g_ptr_array_add(mc->config_stack,current_frame);
+
+ yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
+ }
+
+ BEGIN OUT;
+}
+
+<<EOF>> {
+ if ( --include_stack_ptr < 0 ) {
+ yyterminate();
+ } else {
+ yy_delete_buffer( YY_CURRENT_BUFFER );
+ yy_switch_to_buffer( include_stack[include_stack_ptr] );
+
+ g_free(current_frame->filename);
+ g_free(current_frame);
+ current_frame = g_ptr_array_remove_index(mc->config_stack,mc->config_stack->len-1);
+ }
+}
+
+<OUT>{comment} ;
+
+<OUT>{blk_cmnt_start} BEGIN COMMENT;
+<COMMENT>{cmnt_char} ;
+<COMMENT>{blk_cmnt_stop} BEGIN OUT;
+
+<OUT>{pdu_kw} MATE_PARSE(TOKEN_PDU_KW);
+<OUT>{gop_kw} MATE_PARSE(TOKEN_GOP_KW);
+<OUT>{gog_kw} MATE_PARSE(TOKEN_GOG_KW);
+<OUT>{transform_kw} MATE_PARSE(TOKEN_TRANSFORM_KW);
+<OUT>{match_kw} MATE_PARSE(TOKEN_MATCH_KW);
+<OUT>{strict_kw} MATE_PARSE(TOKEN_STRICT_KW);
+<OUT>{every_kw} MATE_PARSE(TOKEN_EVERY_KW);
+<OUT>{loose_kw} MATE_PARSE(TOKEN_LOOSE_KW);
+<OUT>{replace_kw} MATE_PARSE(TOKEN_REPLACE_KW);
+<OUT>{insert_kw} MATE_PARSE(TOKEN_INSERT_KW);
+<OUT>{gop_tree_kw} MATE_PARSE(TOKEN_GOP_TREE_KW);
+<OUT>{member_kw} MATE_PARSE(TOKEN_MEMBER_KW);
+<OUT>{on_kw} MATE_PARSE(TOKEN_ON_KW);
+<OUT>{start_kw} MATE_PARSE(TOKEN_START_KW);
+<OUT>{stop_kw} MATE_PARSE(TOKEN_STOP_KW);
+<OUT>{extra_kw} MATE_PARSE(TOKEN_EXTRA_KW);
+<OUT>{show_tree_kw} MATE_PARSE(TOKEN_SHOW_TREE_KW);
+<OUT>{show_times_kw} MATE_PARSE(TOKEN_SHOW_TIMES_KW);
+<OUT>{expiration_kw} MATE_PARSE(TOKEN_EXPIRATION_KW);
+<OUT>{idle_timeout_kw} MATE_PARSE(TOKEN_IDLE_TIMEOUT_KW);
+<OUT>{lifetime_kw} MATE_PARSE(TOKEN_LIFETIME_KW);
+<OUT>{no_tree_kw} MATE_PARSE(TOKEN_NO_TREE_KW);
+<OUT>{pdu_tree_kw} MATE_PARSE(TOKEN_PDU_TREE_KW);
+<OUT>{frame_tree_kw} MATE_PARSE(TOKEN_FRAME_TREE_KW);
+<OUT>{basic_tree_kw} MATE_PARSE(TOKEN_BASIC_TREE_KW);
+<OUT>{true_kw} MATE_PARSE(TOKEN_TRUE_KW);
+<OUT>{false_kw} MATE_PARSE(TOKEN_FALSE_KW);
+<OUT>{proto_kw} MATE_PARSE(TOKEN_PROTO_KW);
+<OUT>{payload_kw} MATE_PARSE(TOKEN_PAYLOAD_KW);
+<OUT>{transport_kw} MATE_PARSE(TOKEN_TRANSPORT_KW);
+<OUT>{criteria_kw} MATE_PARSE(TOKEN_CRITERIA_KW);
+<OUT>{accept_kw} MATE_PARSE(TOKEN_ACCEPT_KW);
+<OUT>{reject_kw} MATE_PARSE(TOKEN_REJECT_KW);
+<OUT>{extract_kw} MATE_PARSE(TOKEN_EXTRACT_KW);
+<OUT>{from_kw} MATE_PARSE(TOKEN_FROM_KW);
+<OUT>{drop_unassigned_kw} MATE_PARSE(TOKEN_DROP_UNASSIGNED_KW);
+<OUT>{discard_pdu_data_kw} MATE_PARSE(TOKEN_DISCARD_PDU_DATA_KW);
+<OUT>{last_pdu_kw} MATE_PARSE(TOKEN_LAST_PDU_KW);
+<OUT>{done_kw} MATE_PARSE(TOKEN_DONE_KW);
+
+<OUT>{open_parens} MATE_PARSE(TOKEN_OPEN_PARENS);
+<OUT>{close_parens} MATE_PARSE(TOKEN_CLOSE_PARENS);
+<OUT>{open_brace} MATE_PARSE(TOKEN_OPEN_BRACE);
+<OUT>{close_brace} MATE_PARSE(TOKEN_CLOSE_BRACE);
+<OUT>{comma} MATE_PARSE(TOKEN_COMMA);
+<OUT>{semicolon} MATE_PARSE(TOKEN_SEMICOLON);
+<OUT>{slash} MATE_PARSE(TOKEN_SLASH);
+<OUT>{pipe} MATE_PARSE(TOKEN_PIPE);
+
+<OUT>{integer} MATE_PARSE(TOKEN_INTEGER);
+<OUT>{floating} MATE_PARSE(TOKEN_FLOATING);
+<OUT>{doted_ip} MATE_PARSE(TOKEN_DOTED_IP);
+<OUT>{colonized} MATE_PARSE(TOKEN_COLONIZED);
+<OUT>{name} MATE_PARSE(TOKEN_NAME);
+<OUT>{avp_operator} MATE_PARSE(TOKEN_AVP_OPERATOR);
+
+
+<OUT>{quote} BEGIN QUOTED;
+<QUOTED>{not_quoted} MATE_PARSE(TOKEN_QUOTED);
+<QUOTED>{quote} BEGIN OUT;
+
+%%
+
+extern gboolean mate_load_config(gchar* filename, mate_config* matecfg) {
+ gboolean state;
+ mc = matecfg;
+
+ yyin = fopen(filename,"r");
+
+ if (!yyin) {
+ g_string_sprintfa(mc->config_error,"Mate parser: Could not open file: '%s', error: %s", filename, strerror(errno) );
+ return FALSE;
+ }
+
+ mc->config_stack = g_ptr_array_new();
+
+ current_frame = g_malloc(sizeof(mate_config_frame));
+ current_frame->filename = g_strdup(filename);
+ current_frame->linenum = 1;
+
+ g_ptr_array_add(mc->config_stack,current_frame);
+
+ pParser = MateParserAlloc(g_malloc);
+
+ /* MateParserTrace(stdout,""); */
+
+ TRY {
+ BEGIN OUT;
+
+ yylex();
+
+ MateParser(pParser, 0, NULL,mc);
+
+ yyrestart(NULL);
+
+ MateParserFree(pParser, g_free );
+
+ g_free(current_frame->filename);
+ g_free(current_frame);
+
+ g_ptr_array_free(mc->config_stack,FALSE);
+ state = TRUE;
+ } CATCH(MateConfigError) {
+ state = FALSE;
+ } CATCH_ALL {
+ state = FALSE;
+ g_string_sprintfa(mc->config_error,"An unexpected error occurred");
+ }
+ ENDTRY;
+
+ return state;
+}