tizen 2.3.1 release
[external/ragel.git] / ragel / rlparse.kh
1 /*
2  *  Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
3  */
4
5 /*  This file is part of Ragel.
6  *
7  *  Ragel is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  * 
12  *  Ragel is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  * 
17  *  You should have received a copy of the GNU General Public License
18  *  along with Ragel; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
20  */
21
22 #ifndef _RLPARSE_H
23 #define _RLPARSE_H
24
25 #include <iostream>
26 #include "avltree.h"
27 #include "parsedata.h"
28
29
30 /* Import scanner tokens. */
31 #define IMP_Word 128
32 #define IMP_Literal 129
33 #define IMP_UInt 130
34 #define IMP_Define 131
35
36 /* This is used for tracking the include files/machine pairs. */
37 struct IncludeHistoryItem
38 {
39         IncludeHistoryItem( const char *fileName, const char *sectionName )
40                 : fileName(fileName), sectionName(sectionName) {}
41
42         const char *fileName;
43         const char *sectionName;
44 };
45
46 typedef Vector<IncludeHistoryItem> IncludeHistory;
47
48 struct Parser
49 {
50 %%{
51         parser Parser;
52
53         # General tokens.
54         token TK_Word, TK_Literal, TK_Number, TK_EndSection, TK_UInt, TK_Hex,
55                 TK_Word, TK_Literal, TK_DotDot, TK_ColonGt, TK_ColonGtGt, TK_LtColon,
56                 TK_Arrow, TK_DoubleArrow, TK_StarStar, TK_ColonEquals, TK_NameSep,
57                 TK_BarStar, TK_DashDash;
58
59         # Conditions.
60         token TK_StartCond, TK_AllCond, TK_LeavingCond;
61
62         # State embedding actions.
63         token TK_Middle;
64
65         # Global error actions.
66         token TK_StartGblError, TK_AllGblError, TK_FinalGblError,
67                 TK_NotFinalGblError, TK_NotStartGblError, TK_MiddleGblError;
68
69         # Local error actions.
70         token TK_StartLocalError, TK_AllLocalError, TK_FinalLocalError,
71                 TK_NotFinalLocalError, TK_NotStartLocalError, TK_MiddleLocalError;
72
73         # EOF Action embedding.
74         token TK_StartEOF, TK_AllEOF, TK_FinalEOF, TK_NotFinalEOF, TK_NotStartEOF,
75                 TK_MiddleEOF;
76
77         # To State Actions.
78         token TK_StartToState, TK_AllToState, TK_FinalToState, TK_NotFinalToState,
79                 TK_NotStartToState, TK_MiddleToState;
80
81         # In State Actions.
82         token TK_StartFromState, TK_AllFromState, TK_FinalFromState,
83                 TK_NotFinalFromState, TK_NotStartFromState, TK_MiddleFromState;
84
85         # Regular expression tokens. */
86         token RE_Slash, RE_SqOpen, RE_SqOpenNeg, RE_SqClose, RE_Dot, RE_Star,
87                 RE_Dash, RE_Char;
88
89         # Tokens specific to inline code.
90         token IL_WhiteSpace, IL_Comment, IL_Literal, IL_Symbol;
91
92         # Keywords.
93         token KW_Machine, KW_Include, KW_Import, KW_Write, KW_Action, KW_AlphType,
94                 KW_Range, KW_GetKey, KW_Include, KW_Write, KW_Machine, KW_InWhen,
95                 KW_When, KW_OutWhen, KW_Eof, KW_Err, KW_Lerr, KW_To, KW_From,
96                 KW_Export, KW_PrePush, KW_PostPop, KW_Length;
97
98         # Specials in code blocks.
99         token KW_Break, KW_Exec, KW_Hold, KW_PChar, KW_Char, KW_Goto, KW_Call,
100                 KW_Ret, KW_CurState, KW_TargState, KW_Entry, KW_Next, KW_Exec,
101                 KW_Variable, KW_Access;
102 }%%
103
104         %% write instance_data;
105
106         void init();
107         int parseLangEl( int type, const Token *token );
108
109         Parser( const char *fileName, char *sectionName, InputLoc &sectionLoc )
110                 : sectionName(sectionName)
111         {
112                 pd = new ParseData( fileName, sectionName, sectionLoc );
113                 exportContext.append( false );
114                 includeHistory.append( IncludeHistoryItem( 
115                                 fileName, sectionName ) );
116         }
117
118         int token( InputLoc &loc, int tokId, char *tokstart, int toklen );
119         void tryMachineDef( InputLoc &loc, char *name, 
120                 MachineDef *machineDef, bool isInstance );
121
122         /* Report an error encountered by the parser. */
123         ostream &parse_error( int tokId, Token &token );
124
125         ParseData *pd;
126
127         /* The name of the root section, this does not change during an include. */
128         char *sectionName;
129
130         NameRef nameRef;
131         NameRefList nameRefList;
132
133         Vector<bool> exportContext;
134         IncludeHistory includeHistory;
135
136         Parser *prev, *next;
137 };
138
139 %% write token_defs;
140
141 #endif