File: asm_parse.y

package info (click to toggle)
global 6.6.14-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,612 kB
  • sloc: ansic: 132,180; sh: 5,017; javascript: 4,891; perl: 811; lisp: 676; makefile: 340; yacc: 122
file content (169 lines) | stat: -rw-r--r-- 3,882 bytes parent folder | download | duplicates (2)
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
%{
/*
 * Copyright (c) 2004, 2010 Tama Communications Corporation
 *
 * This file is part of GNU GLOBAL.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <assert.h>
#include <ctype.h>
#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif

#include "internal.h"
#include "die.h"
#include "linetable.h"
#include "strbuf.h"

#define YYLTYPE		int
#define YYLLOC_DEFAULT(Current, Rhs, N)	((Current) = (Rhs)[1])

#undef PUT
#define PUT(type, tag, lno) do {					\
	const char *line_image = linetable_get(lno, NULL);		\
	char *nl = strchr(line_image, '\n');				\
	if (nl != NULL)							\
		*nl = '\0';						\
	param->put(type, tag, lno, param->file, line_image, param->arg);\
	if (nl != NULL)							\
		*nl = '\n';						\
} while (0)

#define GET_SYM(offset) (assert((offset) < strbuf_getlen(asm_symtable)),\
			 &strbuf_value(asm_symtable)[offset])

STRBUF *asm_symtable;

static void yyerror(const struct parser_param *, const char *);

%}

%token ASM_CONST		/* number, string, character */

%token ASM_CALL			/* call, jsr */
%token ASM_ENTRY		/* ENTRY, ALTENTRY, ... */
%token ASM_EXT			/* EXT, SYMBOL_NAME, ... */
%token ASM_SYMBOL
%token ASM_LABEL		/* ^sym */

%token ASM_DEFINE "#define"
%token ASM_UNDEF "#undef"
%token ASM_DIRECTIVE		/* #xxx */

%token ASM_MACRO		/* .macro */
%token ASM_EQU			/* .equ */

%start input
%name-prefix "asm_"
/*
 * This code does not work (GNU Bison 3.8.2).
 *	%define api.prefix {asm_}
 */
%parse-param { const struct parser_param *param }
%lex-param { const struct parser_param *param }

%%

input:	/* empty */
	| input line
;

line:	ASM_ENTRY '(' ASM_SYMBOL ')' error '\n'
		{
			PUT(PARSER_REF_SYM, GET_SYM($1), @1);
			PUT(PARSER_DEF, GET_SYM($3), @3);
			strbuf_reset(asm_symtable);
		}
	| ASM_CALL ASM_SYMBOL error '\n'
		{
			const char *sym = GET_SYM($2);

			if (sym[0] == '_') {
				int c = (unsigned char)sym[1];

				if (isalpha(c) || c == '_' || c >= 0x80)
					PUT(PARSER_REF_SYM, &sym[1], @2);
			}
			strbuf_reset(asm_symtable);
		}
	| ASM_CALL ASM_EXT '(' ASM_SYMBOL ')' error '\n'
		{
			PUT(PARSER_REF_SYM, GET_SYM($2), @2);
			PUT(PARSER_REF_SYM, GET_SYM($4), @4);
			strbuf_reset(asm_symtable);
		}
	| "#define" ASM_SYMBOL error '\n'
		{
			PUT(PARSER_DEF, GET_SYM($2), @2);
			strbuf_reset(asm_symtable);
		}
	| "#undef" ASM_SYMBOL error '\n'
		{
			PUT(PARSER_DEF, GET_SYM($2), @2);
			strbuf_reset(asm_symtable);
		}
	| ASM_MACRO ASM_SYMBOL error '\n'
		{
			PUT(PARSER_DEF, GET_SYM($2), @2);
			strbuf_reset(asm_symtable);
		}
	| ASM_LABEL ASM_MACRO error '\n'
		{
			PUT(PARSER_DEF, GET_SYM($1), @1);
			strbuf_reset(asm_symtable);
		}
	| ASM_EQU ASM_SYMBOL ',' error '\n'
		{
			PUT(PARSER_DEF, GET_SYM($2), @2);
			strbuf_reset(asm_symtable);
		}
	| ASM_LABEL ASM_EQU error '\n'
		{
			PUT(PARSER_DEF, GET_SYM($1), @1);
			strbuf_reset(asm_symtable);
		}
	| error '\n'
		{ strbuf_reset(asm_symtable); }
;

%%

void
assembly(const struct parser_param *param)
{
	if (linetable_open(param->file) == -1)
		die("'%s' cannot open.", param->file);

	asm_symtable = strbuf_open(0);
	asm_initscan();

	asm_parse(param);

	strbuf_close(asm_symtable);
	linetable_close();
}

static void
yyerror(const struct parser_param *param, const char *s)
{

}