summaryrefslogtreecommitdiff
path: root/libfsm/xmlparse.kh
diff options
context:
space:
mode:
Diffstat (limited to 'libfsm/xmlparse.kh')
-rw-r--r--libfsm/xmlparse.kh211
1 files changed, 211 insertions, 0 deletions
diff --git a/libfsm/xmlparse.kh b/libfsm/xmlparse.kh
new file mode 100644
index 00000000..1b0b30ad
--- /dev/null
+++ b/libfsm/xmlparse.kh
@@ -0,0 +1,211 @@
+/*
+ * Copyright 2001-2007 Adrian Thurston <thurston@colm.net>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef _XMLPARSE_H
+#define _XMLPARSE_H
+
+#include "vector.h"
+#include "gendata.h"
+#include "buffer.h"
+#include <iostream>
+
+using std::istream;
+using std::ostream;
+
+#define XML_BUFSIZE 4096
+
+struct AttrMarker
+{
+ char *id;
+ int idLen;
+ char *value;
+ int valueLen;
+};
+
+struct Attribute
+{
+ char *id;
+ char *value;
+};
+
+typedef Vector<AttrMarker> AttrMkList;
+typedef Vector<Attribute> AttrList;
+struct XMLTagHashPair;
+
+struct XMLTag
+{
+ enum TagType { Open, Close };
+
+ XMLTag( XMLTagHashPair *tagId, TagType type ) :
+ tagId(tagId), type(type),
+ content(0), attrList(0) {}
+
+ Attribute *findAttr( const char *id )
+ {
+ if ( attrList != 0 ) {
+ for ( AttrList::Iter attr = *attrList; attr.lte(); attr++ ) {
+ if ( strcmp( id, attr->id ) == 0 )
+ return attr;
+ }
+ }
+ return 0;
+ }
+
+ XMLTagHashPair *tagId;
+ TagType type;
+
+ /* Content is associtated with closing tags. */
+ char *content;
+
+ /* Attribute lists are associated with opening tags. */
+ AttrList *attrList;
+};
+
+
+struct XMLTagHashPair
+{
+ const char *name;
+ int id;
+};
+
+struct Token;
+
+struct GenInlineItem;
+struct GenInlineList;
+
+struct LmSwitchVect;
+struct LmSwitchAction;
+
+struct XmlScanner
+{
+ XmlScanner( const char *fileName, istream &input );
+
+ int scan();
+ void adjustAttrPointers( int distance );
+ std::ostream &error();
+
+ const char *fileName;
+ istream &input;
+
+ /* Scanner State. */
+ int cs, act, have, curline, curcol;
+ char *ts, *te;
+ char *p, *pe;
+ int done;
+
+ /* Token data */
+ char *data;
+ int data_len;
+ int value;
+ AttrMkList attrMkList;
+ Buffer buffer;
+ char *tag_id_start;
+ int tag_id_len;
+ int token_col, token_line;
+
+ char buf[XML_BUFSIZE];
+};
+
+
+struct XmlParser
+{
+ %%{
+ parser XmlParser;
+
+ token TAG_unknown, TAG_ragel, TAG_ragel_def, TAG_host, TAG_state_list,
+ TAG_state, TAG_trans_list, TAG_t, TAG_machine, TAG_start_state,
+ TAG_error_state, TAG_action_list, TAG_action_table_list,
+ TAG_action, TAG_action_table, TAG_alphtype, TAG_element,
+ TAG_getkey, TAG_state_actions, TAG_entry_points, TAG_sub_action,
+ TAG_cond_space_list, TAG_cond_space, TAG_cond_list, TAG_c,
+ TAG_exports, TAG_ex;
+
+ # Inline block tokens.
+ token TAG_text, TAG_goto, TAG_call, TAG_next, TAG_goto_expr,
+ TAG_call_expr, TAG_next_expr, TAG_ret, TAG_pchar, TAG_char,
+ TAG_hold, TAG_exec, TAG_curs, TAG_targs, TAG_entry, TAG_data,
+ TAG_lm_switch, TAG_init_act, TAG_set_act, TAG_set_tokend,
+ TAG_get_tokend, TAG_init_tokstart, TAG_set_tokstart;
+
+ token TAG_write, TAG_access, TAG_break, TAG_arg, TAG_cs_expr;
+
+ token TAG_p_expr, TAG_pe_expr, TAG_eof_expr, TAG_cs_expr, TAG_top_expr,
+ TAG_stack_expr, TAG_act_expr, TAG_tokstart_expr, TAG_tokend_expr,
+ TAG_data_expr, TAG_prepush, TAG_postpop, TAG_eof_t;
+ }%%
+
+ %% write instance_data;
+
+ void init();
+ int parseLangEl( int type, const Token *token );
+
+ XmlParser( const char *sourceFileName, const char *xmlFileName, bool outputActive, bool wantComplete ) :
+ sourceFileName(sourceFileName),
+ fileName(xmlFileName),
+ outStream(0),
+ outputActive(outputActive),
+ wantComplete(wantComplete),
+ cgd(0) { }
+
+ int token( int tokenId, Token &token );
+ int token( int tokenId, int col, int line );
+ int token( XMLTag *tag, int col, int line );
+
+ void openOutput();
+
+ /* Report an error encountered by the parser. */
+ ostream &warning( const InputLoc &loc );
+ ostream &error();
+ ostream &error( const InputLoc &loc );
+ ostream &parser_error( int tokId, Token &token );
+ ostream &source_error( const InputLoc &loc );
+
+ /* The name of the root section, this does not change during an include. */
+ const char *sourceFileName;
+ const char *fileName;
+ ostream *outStream;
+ bool outputActive;
+ bool wantComplete;
+
+ /* Collected during parsing. */
+ char *attrKey;
+ char *attrValue;
+ int curAction;
+ int curActionTable;
+ int curTrans;
+ int curState;
+ int curCondSpace;
+ int curStateCond;
+
+ CodeGenData *cgd;
+ CodeGenMap codeGenMap;
+
+ Vector <char*> writeOptions;
+};
+
+%% write token_defs;
+
+int xml_parse( std::istream &input, const char *fileName,
+ bool outputActive, bool wantComplete,
+ XmlScanner &scanner, XmlParser &parser );
+
+#endif