1 /*
2  * Copyright (c) 2004, 2005 Tama Communications Corporation
3  *
4  * This file is part of GNU GLOBAL.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef _LEXCOMMON_H
20 #define _LEXCOMMON_H
21 
22 #include "incop.h"
23 #include "tab.h"
24 
25 /*
26  * Definition of LEXTEXT, LEXLENG, LEXIN and LEXRESTART.
27  *
28  * These symbols are substitutions of yytext, yyleng, yyin and yyrestart.
29  * You should write lex code using them.
30  * The usage of this file is, for instance, in "c.l":
31  *
32  * [code]
33  *	#define lex_symbol_generation_rule(x) c_ ## x
34  *	#include "lexcommon.h"
35  */
36 #ifndef lex_symbol_generation_rule
37 ERROR: lex_symbol_generation_rule(x) macro not defined.
38 lexcommon.h requires the lex_symbol_generation_rule(x) macro for each language
39 to generate language specific symbols.
40 #endif
41 #define LEXTEXT lex_symbol_generation_rule(text)
42 #define LEXLENG lex_symbol_generation_rule(leng)
43 #define LEXIN lex_symbol_generation_rule(in)
44 #define LEXRESTART lex_symbol_generation_rule(restart)
45 
46 /*
47  * The default action for line control.
48  * These can be applicable to most languages.
49  * You must define C_COMMENT, CPP_COMMENT, SHELL_COMMENT, LITERAL, STRING
50  * and PREPROCESSOR_LINE as %start values, even if they are not used.
51  * It assumed that CPP_COMMENT and SHELL_COMMENT is one line comment.
52  */
53 static int lexcommon_lineno;
54 static int begin_line;
55 
56 /*
57  * If you want newline to terminate string, set this variable to 1.
58  */
59 static int newline_terminate_string = 0;
60 
61 /*
62  * Variables for converting tabs to spaces.
63  */
64 static int dest_column;
65 static int left_spaces;
66 
67 #define YY_INPUT(buf, result, max_size) do {				\
68 	result = read_file_detabing(buf, max_size, LEXIN,		\
69 			&dest_column, &left_spaces);			\
70 } while (0)
71 
72 #define LINENO lexcommon_lineno
73 
74 #define DEFAULT_BEGIN_OF_FILE_ACTION {					\
75         LEXIN = ip;							\
76         LEXRESTART(LEXIN);						\
77         LINENO = 1;							\
78         begin_line = 1;							\
79 	dest_column = 0;						\
80 	left_spaces = 0;						\
81 }
82 
83 #define DEFAULT_YY_USER_ACTION {					\
84 	if (begin_line) {						\
85 		put_begin_of_line(LINENO);				\
86 		switch (YY_START) {					\
87 		case C_COMMENT:						\
88 		case CPP_COMMENT:					\
89 		case SHELL_COMMENT:					\
90 			echos(comment_begin);				\
91 			break;						\
92 		}							\
93 		begin_line = 0;						\
94 	}								\
95 }
96 
97 #define DEFAULT_END_OF_LINE_ACTION {					\
98 	switch (YY_START) {						\
99 	case CPP_COMMENT:						\
100 	case SHELL_COMMENT:						\
101 		yy_pop_state();						\
102 		/* FALLTHROUGH */					\
103 	case C_COMMENT:							\
104 		echos(comment_end);					\
105 		break;							\
106 	case STRING:							\
107 	case LITERAL:							\
108 		if (newline_terminate_string)				\
109 			yy_pop_state();					\
110 		break;							\
111 	}								\
112 	if (YY_START == PREPROCESSOR_LINE)				\
113 		yy_pop_state();						\
114 	put_end_of_line(LINENO);					\
115 	/* for the next line */						\
116 	LINENO++;							\
117 	begin_line = 1;							\
118 }
119 
120 #define DEFAULT_BACKSLASH_NEWLINE_ACTION {				\
121 	echoc('\\');							\
122 	switch (YY_START) {						\
123 	case CPP_COMMENT:						\
124 	case C_COMMENT:							\
125 	case SHELL_COMMENT:						\
126 		echos(comment_end);					\
127 		break;							\
128 	}								\
129 	put_end_of_line(LINENO);					\
130 	/* for the next line */						\
131 	LINENO++;							\
132 	begin_line = 1;							\
133 }
134 
135 /*
136  * Output routine.
137  */
138 extern void echoc(int);
139 extern void echos(const char *);
140 extern const char *generate_guide(int);
141 extern void put_anchor(char *, int, int);
142 extern void put_anchor_force(char *, int, int);
143 extern void put_include_anchor(struct data *, const char *);
144 extern void put_include_anchor_direct(const char *, const char *);
145 extern void put_reserved_word(const char *);
146 extern void put_macro(const char *);
147 extern void unknown_preprocessing_directive(const char *, int);
148 extern void unexpected_eof(int);
149 extern void unknown_yacc_directive(const char *, int);
150 extern void missing_left(const char *, int);
151 extern void put_char(int);
152 extern void put_string(const char *);
153 extern void put_brace(const char *);
154 extern void put_begin_of_line(int);
155 extern void put_end_of_line(int);
156 
157 #endif /* ! _LEXCOMMON_H */
158