[go: up one dir, main page]

Menu

[2e2bd2]: / src / yparser.cc  Maximize  Restore  History

Download this file

161 lines (128 with data), 3.5 kB

  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
/*
* IceWM - Generic parser
* Copyright (C) 2001 The Authors of IceWM
*
* Release under terms of the GNU Library General Public License
*
* 2001/01/25: Mathias Hasselmann <mathias.hasselmann@gmx.net>
* - initial version
* 2001/01/27: Mathias Hasselmann <mathias.hasselmann@gmx.net>
* - specialized error message methods
* - splitted skipSpaces into skipBlanks and skipWhitespace
*/
#include "base.h"
#include "intl.h"
#include "yparser.h"
#include <cerrno>
#include <cstring>
#include <cctype>
#include <unistd.h>
int YParser::parse(const char * filename) {
bool isExecutable(access(filename, X_OK) == 0);
if ((fStream = isExecutable ? popen(filename, "r")
: fopen(filename, "r")) == NULL) {
warn("%s: %s", filename, strerror(errno));
return errno;
}
fFilename = filename;
nextChar();
parseStream();
if (isExecutable)
pclose(fStream);
else
fclose(fStream);
fStream = NULL;
fFilename = NULL;
fLine = 0;
fColumn = 0;
return 0;
}
int YParser::nextChar() {
if (fStream)
if ((fChar = fgetc(fStream)) == '\n' || fLine == 0)
++fLine, fColumn = 0;
else
++fColumn;
else
fChar = EOF;
return fChar;
}
unsigned YParser::skipBlanks() {
unsigned spaces(0);
while (good() && isspace(currChar()) && currChar() != '\n') {
nextChar(); ++spaces;
}
if (currChar() == '#') skipLine();
return spaces;
}
unsigned YParser::skipWhitespace() {
unsigned spaces(0);
while (good() && (isspace(currChar()) || '#' == currChar())) {
if (currChar() == '#') skipLine();
else nextChar();
++spaces;
}
return spaces;
}
void YParser::skipLine() {
while (good() && currChar() != '\n') nextChar(); nextChar();
}
char * YParser::getIdentifier(char * buf, const size_t len) {
size_t pos(0);
while (isalnum(currChar()) && pos < len - 1) {
buf[pos++] = currChar(); nextChar();
}
buf[pos] = '\0';
return buf;
}
char * YParser::getString(char * buf, const size_t len) {
size_t pos(0);
bool instr(false);
while ((instr || currChar() == '"' || !isspace(currChar())) &&
pos < len - 1) {
if (currChar() == '\\')
switch (nextChar()) {
default: buf[pos++] = currChar(); break;
case 'a': buf[pos++] = '\a'; break;
case 'b': buf[pos++] = '\b'; break;
case 'e': buf[pos++] = 27; break;
case 'f': buf[pos++] = '\f'; break;
case 'n': buf[pos++] = '\n'; break;
case 'r': buf[pos++] = '\r'; break;
case 't': buf[pos++] = '\t'; break;
case 'v': buf[pos++] = '\v'; break;
case 'x': {
int a, b;
if ((a = unhex(nextChar())) != -1 &&
(b = unhex(nextChar())) != -1)
buf[pos++] = (unsigned char)((a << 4) + b);
else
parseError(_("Pair of hexadecimal digits expected"));
break;
}
}
else if (currChar() == '"')
instr = !instr;
else
buf[pos++] = currChar();
nextChar();
}
buf[pos] = '\0';
return buf;
}
void YParser::parseError(const char * what) {
warn("%s:%d:%d: %s", fFilename, fLine, fColumn, what);
}
void YParser::unexpectedIdentifier(const char * id) {
char * msg = strJoin(_("Unexpected identifier"), ": »", id, "«", NULL);
parseError(msg);
delete[] msg;
}
void YParser::identifierExpected() {
char * msg = strJoin(_("Identifier expected"), NULL);
parseError(msg);
delete[] msg;
}
void YParser::separatorExpected() {
parseError(_("Separator expected"));
}