summaryrefslogtreecommitdiff
path: root/colm/lmparse.kh
blob: c32c4251696a02efc1c85a0889c1e95155af925e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 *  Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
 */

/*  This file is part of Colm.
 *
 *  Colm 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.
 * 
 *  Colm 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 Colm; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

#ifndef RLPARSE_H
#define RLPARSE_H

#include <iostream>
#include "avltree.h"
#include "parsedata.h"

#define PROPERTY_REDUCE_FIRST 0x1

struct ColmParser
{
	ColmParser( Compiler *pd, const char *fileName, const char *sectionName, const InputLoc &sectionLoc )
		: pd(pd), sectionName(sectionName), enterRl(false)
	{}

	%%{
	parser ColmParser;

	# Use a class for tokens.
	token uses class Token;

	# Atoms.
	token TK_Word, TK_Literal, TK_SingleLit, TK_DoubleLit, TK_Number, TK_UInt,
		TK_Hex, KW_Nil, KW_True, KW_False;

	# General tokens.
	token TK_DotDot, TK_ColonGt, TK_ColonGtGt, TK_LtColon,
		TK_DoubleArrow, TK_StarStar, TK_NameSep, TK_DashDash, TK_DoubleEql,
		TK_NotEql, TK_DoubleColon, TK_LessEql, TK_GrtrEql, TK_RightArrow,
		TK_LitPat, TK_AmpAmp, TK_BarBar, TK_SqOpen, TK_SqOpenNeg, TK_SqClose,
		TK_Dash, TK_ReChar, TK_LtLt;

	# Defining things.
	token KW_Rl, KW_Def, KW_Lex, KW_Context, KW_Ignore, KW_Token, KW_Commit, KW_Namespace, KW_End,
		KW_Literal, KW_ReduceFirst, KW_Map, KW_List, KW_Vector, KW_Parser, KW_Global, KW_Export,
		KW_Iter, KW_Reject, KW_Ptr, KW_Ref, KW_Deref;

	# Language.
	token KW_If, KW_While, KW_Else, KW_Elsif, KW_For, KW_Return, KW_Yield, KW_In,
		KW_Break, KW_PrintXMLAC, KW_PrintXML, KW_Print, KW_PrintStream, KW_Require;

	# Patterns.
	token KW_Match, KW_Construct, KW_Parse, KW_ParseStop, KW_New, KW_MakeToken,
		KW_MakeTree, KW_TypeId, KW_Alias, KW_Send, KW_Ni;

	token KW_Include, KW_Preeof;

	token KW_Left, KW_Right, KW_Nonassoc, KW_Prec;

	}%%

	%% write instance_data;

	Compiler *pd;

	/* The name of the root section, this does not change during an include. */
	const char *sectionName;

	RegionSetVect regionStack;
	NamespaceVect namespaceStack;
	ContextStack contextStack;

	ReCaptureVect reCaptureVect;

	/* Lexical feedback. */
	bool enterRl;

	void init();
	void initPda();
	int parseLangEl( int type, const Token *token );
	int token( InputLoc &loc, int tokId, char *tokstart, int toklen );
	void addRegularDef( const InputLoc &loc, Namespace *nspace, 
			const String &name, LexJoin *join );
	TokenRegion *createRegion( const InputLoc &loc );
	Namespace *createNamespace( const InputLoc &loc, const String &name );
	void pushRegionSet( const InputLoc &loc );
	void popRegionSet();
	void addProduction( const InputLoc &loc, const String &name, 
			ProdElList *prodElList, bool commit, CodeBlock *redBlock, LangEl *predOf );
	void addArgvList();
	LexJoin *literalJoin( const InputLoc &loc, const String &data );

	/* Report an error encountered by the parser. */
	ostream &parse_error( int tokId, Token &token );

	void tokenDef( const InputLoc &loc, String name, LexJoin *join, ObjectDef *objectDef,
			CodeBlock *transBlock, bool ignore, bool noPreIgnore, bool noPostIgnore );

	void zeroDef( const InputLoc &loc, const String &data,
			bool noPreIgnore, bool noPostIgnore );
	void literalDef( const InputLoc &loc, const String &data,
			bool noPreIgnore, bool noPostIgnore );

	ObjectDef *blockOpen();
	void blockClose();
	void functionDef( StmtList *stmtList, ObjectDef *localFrame,
			ParameterList *paramList, TypeRef *typeRef, const String &name );
	void iterDef( StmtList *stmtList, ObjectDef *localFrame,
			ParameterList *paramList, const String &name );
	LangStmt *globalDef( ObjectField *objField, LangExpr *expr, LangStmt::Type assignType );
	void cflDef( NtDef *ntDef, ObjectDef *objectDef, LelDefList *defList );
	ReOrBlock *lexRegularExprData( ReOrBlock *reOrBlock, ReOrItem *reOrItem );

	int lexFactorRepNum( const InputLoc &loc, const String &data );
	LexFactor *lexRlFactorName( const String &data, const InputLoc &loc );
	LexFactorAug *lexFactorLabel( const InputLoc &loc, const String &data, LexFactorAug *factorAug );
	LexJoin *lexOptJoin( LexJoin *join, LexJoin *context );
	LangExpr *send( const InputLoc &loc, LangVarRef *varRef, ConsItemList *list );
	LangExpr *parseCmd( const InputLoc &loc, bool stop, ObjectField *objField,
			TypeRef *typeRef, FieldInitVect *fieldInitVect, ConsItemList *list );
	PatternItemList *patternEl( LangVarRef *varRef, PatternItemList *list );
	PatternItemList *patternElNamed( const InputLoc &loc, NamespaceQual *nspaceQual,
			const String &data, RepeatType repeatType );
	PatternItemList *patternElType( const InputLoc &loc,
			NamespaceQual *nspaceQual, const String &data, RepeatType repeatType );
	PatternItemList *patListConcat( PatternItemList *list1, PatternItemList *list2 );
	ConsItemList *consListConcat( ConsItemList *list1, ConsItemList *list2 );
	LangStmt *forScope( const InputLoc &loc, const String &data,
			TypeRef *typeRef, LangTerm *langTerm, StmtList *stmtList );
	void preEof( const InputLoc &loc, StmtList *stmtList, ObjectDef *localFrame );

	ProdEl *prodElName( const InputLoc &loc, const String &data, NamespaceQual *nspaceQual,
			ObjectField *objField, RepeatType repeatType, bool commit );
	ProdEl *prodElLiteral( const InputLoc &loc, const String &data, NamespaceQual *nspaceQual,
			ObjectField *objField, RepeatType repeatType, bool commit );
	ConsItemList *consElLiteral( const InputLoc &loc,
			const String &data, NamespaceQual *nspaceQual );
	Production *production( const InputLoc &loc, ProdElList *prodElList,
			bool commit, CodeBlock *codeBlock, LangEl *predOf );
	void objVarDef( ObjectDef *objectDef, ObjectField *objField );
	LelDefList *prodAppend( LelDefList *defList, Production *definition );

	LangExpr *construct( const InputLoc &loc, ObjectField *objField,
			ConsItemList *list, TypeRef *typeRef, FieldInitVect *fieldInitVect );
	LangExpr *match( const InputLoc &loc, LangVarRef *varRef,
			PatternItemList *list );
	LangStmt *varDef( ObjectField *objField,
			LangExpr *expr, LangStmt::Type assignType );
	LangStmt *exportStmt( ObjectField *objField, LangStmt::Type assignType, LangExpr *expr );

	LangExpr *require( const InputLoc &loc, LangVarRef *varRef, PatternItemList *list );
	void contextVarDef( const InputLoc &loc, ObjectField *objField );
	void contextHead( const InputLoc &loc, const String &data );
	void tokenInstanceName( const String &name );
	StmtList *appendStatement( StmtList *stmtList, LangStmt *stmt );
	ParameterList *appendParam( ParameterList *paramList, ObjectField *objField );
	ObjectField *addParam( const InputLoc &loc, TypeRef *typeRef, const String &name );
	PredDecl *predTokenName( const InputLoc &loc, NamespaceQual *qual, const String &data );
	PredDecl *predTokenLit( const InputLoc &loc, const String &data, NamespaceQual *nspaceQual );
	void alias( const InputLoc &loc, const String &data, TypeRef *typeRef );
	void precedenceStmt( PredType predType, PredDeclList *predDeclList );

	void parseInput( StmtList *stmtList );
	void printParseTree( StmtList *stmtList );
	void printParseTree();
	void go();
};

%% write token_defs;

#endif