2 * Copyright (c) 2013 David Chisnall
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include "input_buffer.hh"
45 input_buffer::skip_spaces()
47 if (cursor >= size) { return; }
48 if (cursor < 0) { return; }
49 char c = buffer[cursor];
50 while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\f')
51 || (c == '\v') || (c == '\r'))
66 input_buffer::buffer_from_offset(int offset, int s)
74 return input_buffer();
76 if (s > (size-offset))
78 return input_buffer();
80 return input_buffer(&buffer[offset], s);
84 input_buffer::consume(const char *str)
86 int len = strlen(str);
87 if (len > size - cursor)
93 for (int i=0 ; i<len ; ++i)
95 if (str[i] != buffer[cursor + i])
107 input_buffer::consume_integer(long long &outInt)
109 // The first character must be a digit. Hex and octal strings
110 // are prefixed by 0 and 0x, respectively.
111 if (!isdigit((*this)[0]))
116 outInt = strtoll(&buffer[cursor], &end, 0);
117 if (end == &buffer[cursor])
121 cursor = end - buffer;
126 input_buffer::consume_hex_byte(uint8_t &outByte)
128 if (!ishexdigit((*this)[0]) && !ishexdigit((*this)[1]))
132 outByte = (digittoint((*this)[0]) << 4) | digittoint((*this)[1]);
138 input_buffer::next_token()
145 if (((*this)[0] == '/') && ((*this)[1] == '*'))
147 // eat the start of the comment
151 // Find the ending * of */
152 while ((**this != '\0') && (**this != '*'))
158 } while ((**this != '\0') && (**this != '/'));
163 if (((*this)[0] == '/') && ((*this)[1] == '/'))
165 // eat the start of the comment
168 // Find the ending * of */
169 while (**this != '\n')
176 } while (start != cursor);
181 input_buffer::parse_error(const char *msg)
185 int line_end = cursor;
186 for (int i=cursor ; i>0 ; --i)
188 if (buffer[i] == '\n')
197 for (int i=cursor+1 ; i<size ; ++i)
199 if (buffer[i] == '\n')
205 fprintf(stderr, "Error on line %d: %s\n", line_count, msg);
206 fwrite(&buffer[line_start], line_end-line_start, 1, stderr);
208 for (int i=0 ; i<(cursor-line_start) ; ++i)
218 fprintf(stderr, "Current cursor: %d\n", cursor);
219 fwrite(&buffer[cursor], size-cursor, 1, stderr);
222 mmap_input_buffer::mmap_input_buffer(int fd) : input_buffer(0, 0)
227 perror("Failed to stat file");
230 buffer = (const char*)mmap(0, size, PROT_READ,
231 MAP_PREFAULT_READ, fd, 0);
234 perror("Failed to mmap file");
238 mmap_input_buffer::~mmap_input_buffer()
242 munmap((void*)buffer, size);
246 stream_input_buffer::stream_input_buffer() : input_buffer(0, 0)
249 while ((c = fgetc(stdin)) != EOF)