* move imageManager code into new imageManager.h/imageManager.cpp
[openc2e.git] / lexutil.h
blob09c7d51638629f243e29ddb4ac2f8c1f94050bfc
1 /*
2 * lexutil.h
3 * openc2e
5 * Created by Bryan Donlan on Thu 11 Aug 2005.
6 * Copyright (c) 2005 Bryan Donlan. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
19 #ifndef LEXUTIL_H
20 #define LEXUTIL_H 1
22 #include <cstdlib>
23 #include <cstddef>
24 #include <cstring>
25 #include <string>
26 #include <cstdio>
27 #include <cctype>
28 #include <vector>
29 #include <algorithm>
31 #include "token.h"
33 extern int lex_lineno;
35 void lexreset();
37 extern bytestring_t bytestr;
38 extern std::string temp_str;
40 static inline int make_int(int v) {
41 lasttok.payload = caosVar(v);
42 return 1;
45 static inline int make_bin(const char *str) {
46 int temp = 0, pos;
47 str++; // skip %
48 for (pos = 0; pos < 8; pos++) {
49 temp <<= 1;
50 temp += str[pos] - '0';
52 return make_int(temp);
53 // return temp;
56 static inline int make_float(float f) {
57 lasttok.payload = caosVar(f);
58 return 1;
61 static inline int make_word(const char *str) {
62 std::string result = str;
63 std::transform(result.begin(), result.end(), result.begin(), (int(*)(int))tolower);
64 lasttok.payload = result;
65 return 1;
68 static inline void push_string_escape(char c) {
69 switch (c) {
70 case 'n':
71 temp_str += '\n';
72 break;
73 case 'r':
74 temp_str += '\r';
75 break;
76 case 't':
77 temp_str += '\t';
78 break;
79 default:
80 temp_str += c;
81 break;
85 static inline void push_string_lit(char c) {
86 temp_str += c;
89 static inline int make_string() {
90 lasttok.payload = caosVar(temp_str);
91 temp_str = "";
92 return 1;
95 static inline int push_bytestr(unsigned int bs) {
96 if (bs > 255) {
97 std::ostringstream oss;
98 oss << "Byte string element out of range (0 <= " << bs << " < 256) at line " << lex_lineno;
99 throw parseException(oss.str());
101 bytestr.push_back(bs);
102 return 1;
105 static inline int make_bytestr() {
106 lasttok.payload = bytestr;
107 bytestr.clear();
108 return 1;
111 static inline void parse_error(const char *yytext, int yylineno) {
112 std::ostringstream oss;
113 oss << "Parse error at line " << yylineno << ", near " << yytext;
114 throw parseException(oss.str());
117 #endif
119 /* vim: set noet: */