summaryrefslogtreecommitdiffstats
path: root/src/glsl/glcpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/glsl/glcpp')
-rw-r--r--src/glsl/glcpp/glcpp-lex.l53
-rw-r--r--src/glsl/glcpp/glcpp.c40
-rw-r--r--src/glsl/glcpp/tests/063-comments.c.expected7
3 files changed, 48 insertions, 52 deletions
diff --git a/src/glsl/glcpp/glcpp-lex.l b/src/glsl/glcpp/glcpp-lex.l
index 11b73aea88b..86618870885 100644
--- a/src/glsl/glcpp/glcpp-lex.l
+++ b/src/glsl/glcpp/glcpp-lex.l
@@ -34,6 +34,10 @@
int glcpp_get_column (yyscan_t yyscanner);
void glcpp_set_column (int column_no , yyscan_t yyscanner);
+#ifdef _MSC_VER
+#define YY_NO_UNISTD_H
+#endif
+
#define YY_NO_INPUT
#define YY_USER_ACTION \
@@ -57,7 +61,7 @@ void glcpp_set_column (int column_no , yyscan_t yyscanner);
%option stack
%option never-interactive
-%x DONE COMMENT UNREACHABLE
+%x DONE COMMENT UNREACHABLE SKIP
SPACE [[:space:]]
NONSPACE [^[:space:]]
@@ -74,6 +78,17 @@ OCTAL_INTEGER 0[0-7]*[uU]?
HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
%%
+ /* Implicitly switch between SKIP and INITIAL (non-skipping);
+ * don't switch if some other state was explicitly set.
+ */
+ glcpp_parser_t *parser = yyextra;
+ if (YY_START == 0 || YY_START == SKIP) {
+ if (parser->lexing_if || parser->skip_stack == NULL || parser->skip_stack->type == SKIP_NO_SKIP) {
+ BEGIN 0;
+ } else {
+ BEGIN SKIP;
+ }
+ }
/* Single-line comments */
"//"[^\n]* {
@@ -137,60 +152,44 @@ HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
yylineno = strtol(ptr, &ptr, 0) - 1;
}
-{HASH}ifdef/.*\n {
+<SKIP,INITIAL>{
+{HASH}ifdef {
yyextra->lexing_if = 1;
yyextra->space_tokens = 0;
return HASH_IFDEF;
}
-{HASH}ifndef/.*\n {
+{HASH}ifndef {
yyextra->lexing_if = 1;
yyextra->space_tokens = 0;
return HASH_IFNDEF;
}
-{HASH}if/[^_a-zA-Z0-9].*\n {
+{HASH}if/[^_a-zA-Z0-9] {
yyextra->lexing_if = 1;
yyextra->space_tokens = 0;
return HASH_IF;
}
-{HASH}elif/.*\n {
+{HASH}elif {
yyextra->lexing_if = 1;
yyextra->space_tokens = 0;
return HASH_ELIF;
}
-{HASH}else/.*\n {
+{HASH}else {
yyextra->space_tokens = 0;
return HASH_ELSE;
}
-{HASH}endif/.*\n {
+{HASH}endif {
yyextra->space_tokens = 0;
return HASH_ENDIF;
}
-
- /* When skipping (due to an #if 0 or similar) consume anything
- * up to a newline. We do this with less priority than any
- * #if-related directive (#if, #elif, #else, #endif), but with
- * more priority than any other directive or token to avoid
- * any side-effects from skipped content.
- *
- * We use the lexing_if flag to avoid skipping any part of an
- * if conditional expression. */
-[^\n]+/\n {
- /* Since this rule always matches, YY_USER_ACTION gets called for it,
- * wrongly incrementing yycolumn. We undo that effect here. */
- yycolumn -= yyleng;
- if (yyextra->lexing_if ||
- yyextra->skip_stack == NULL ||
- yyextra->skip_stack->type == SKIP_NO_SKIP)
- {
- REJECT;
- }
}
+<SKIP>[^\n] ;
+
{HASH}error.* {
char *p;
for (p = yytext; !isalpha(p[0]); p++); /* skip " # " */
@@ -293,7 +292,7 @@ HEXADECIMAL_INTEGER 0[xX][0-9a-fA-F]+[uU]?
}
}
-\n {
+<SKIP,INITIAL>\n {
yyextra->lexing_if = 0;
yylineno++;
yycolumn = 0;
diff --git a/src/glsl/glcpp/glcpp.c b/src/glsl/glcpp/glcpp.c
index 564194caac2..325289129d5 100644
--- a/src/glsl/glcpp/glcpp.c
+++ b/src/glsl/glcpp/glcpp.c
@@ -21,10 +21,7 @@
* DEALINGS IN THE SOFTWARE.
*/
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
+#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "glcpp.h"
@@ -40,16 +37,16 @@ _mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
*ptr = sh;
}
-/* Read from fd until EOF and return a string of everything read.
+/* Read from fp until EOF and return a string of everything read.
*/
static char *
-load_text_fd (void *ctx, int fd)
+load_text_fp (void *ctx, FILE *fp)
{
#define CHUNK 4096
char *text = NULL;
- ssize_t text_size = 0;
- ssize_t total_read = 0;
- ssize_t bytes;
+ size_t text_size = 0;
+ size_t total_read = 0;
+ size_t bytes;
while (1) {
if (total_read + CHUNK + 1 > text_size) {
@@ -60,19 +57,12 @@ load_text_fd (void *ctx, int fd)
return NULL;
}
}
- bytes = read (fd, text + total_read, CHUNK);
- if (bytes < 0) {
- fprintf (stderr, "Error while reading: %s\n",
- strerror (errno));
- ralloc_free (text);
- return NULL;
- }
+ bytes = fread (text + total_read, 1, CHUNK, fp);
+ total_read += bytes;
- if (bytes == 0) {
+ if (bytes < CHUNK) {
break;
}
-
- total_read += bytes;
}
text[total_read] = '\0';
@@ -84,21 +74,21 @@ static char *
load_text_file(void *ctx, const char *filename)
{
char *text;
- int fd;
+ FILE *fp;
if (filename == NULL || strcmp (filename, "-") == 0)
- return load_text_fd (ctx, STDIN_FILENO);
+ return load_text_fp (ctx, stdin);
- fd = open (filename, O_RDONLY);
- if (fd < 0) {
+ fp = fopen (filename, "r");
+ if (fp == NULL) {
fprintf (stderr, "Failed to open file %s: %s\n",
filename, strerror (errno));
return NULL;
}
- text = load_text_fd (ctx, fd);
+ text = load_text_fp (ctx, fp);
- close(fd);
+ fclose(fp);
return text;
}
diff --git a/src/glsl/glcpp/tests/063-comments.c.expected b/src/glsl/glcpp/tests/063-comments.c.expected
index ed4feedd457..73ca7071faa 100644
--- a/src/glsl/glcpp/tests/063-comments.c.expected
+++ b/src/glsl/glcpp/tests/063-comments.c.expected
@@ -5,8 +5,15 @@ f = g /h;
l();
m = n
+ p;
+
+
+
+
+
+
more code here
+
are not treated like comments.