From: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>
To: Jon Loeliger <jdl-CYoMK+44s/E@public.gmane.org>
Cc: devicetree-discuss-mnsaURCQ41sdnm+yROfE0A@public.gmane.org
Subject: [3/5] dtc: Cleanup line number tracking, add column number tracking
Date: Sat, 4 Oct 2008 22:27:06 +1000 (EST) [thread overview]
Message-ID: <20081004122706.5956EDDE11@ozlabs.org> (raw)
In-Reply-To: <20081004122157.GT30184-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
Our YYLTYPE current carries around first and last line and first and
last column information. However, of these, on the first line
information is actually filled in properly.
Furthermore, filling in the line number information from yylineno is
kind of clunky: we have to copy its value to the srcfile stack and
back to handle include file positioning correctly.
This patch cleans this up. We turn off flex's yylineno option and
instead track the line and column number ourselves from
YY_USER_ACTION. The line and column number are stored directly inside
the srcfile_state structure, so it's automatically a per-file
quantity. We now also fill in all the yylloc from YY_USER_ACTION.
Signed-off-by: David Gibson <david-xT8FGy+AXnRB3Ne2BGzF6laj5H9X9Tb+@public.gmane.org>
---
dtc-lexer.l | 11 ++++-------
srcpos.c | 33 ++++++++++++++++++++++++++++++++-
srcpos.h | 3 ++-
3 files changed, 38 insertions(+), 9 deletions(-)
Index: dtc/srcpos.c
===================================================================
--- dtc.orig/srcpos.c 2008-10-04 22:15:29.000000000 +1000
+++ dtc/srcpos.c 2008-10-04 22:22:17.000000000 +1000
@@ -87,6 +87,10 @@ void srcfile_push(const char *fname)
srcfile->f = srcfile_relative_open(fname, &srcfile->name);
srcfile->dir = dirname(srcfile->name);
srcfile->prev = current_srcfile;
+
+ srcfile->lineno = 1;
+ srcfile->colno = 1;
+
current_srcfile = srcfile;
}
@@ -99,7 +103,8 @@ int srcfile_pop(void)
current_srcfile = srcfile->prev;
if (fclose(srcfile->f))
- die("Error closing \"%s\": %s\n", srcfile->name, strerror(errno));
+ die("Error closing \"%s\": %s\n", srcfile->name,
+ strerror(errno));
/* FIXME: We allow the srcfile_state structure to leak,
* because it could still be referenced from a location
@@ -122,6 +127,32 @@ srcpos srcpos_empty = {
.file = NULL,
};
+#define TAB_SIZE 8
+
+void srcpos_update(srcpos *pos, const char *text, int len)
+{
+ int i;
+
+ pos->file = current_srcfile;
+
+ pos->first_line = current_srcfile->lineno;
+ pos->first_column = current_srcfile->colno;
+
+ for (i = 0; i < len; i++)
+ if (text[i] == '\n') {
+ current_srcfile->lineno++;
+ current_srcfile->colno = 1;
+ } else if (text[i] == '\t') {
+ current_srcfile->colno =
+ ALIGN(current_srcfile->colno, TAB_SIZE);
+ } else {
+ current_srcfile->colno++;
+ }
+
+ pos->last_line = current_srcfile->lineno;
+ pos->last_column = current_srcfile->colno;
+}
+
srcpos *
srcpos_copy(srcpos *pos)
{
Index: dtc/dtc-lexer.l
===================================================================
--- dtc.orig/dtc-lexer.l 2008-10-04 22:15:29.000000000 +1000
+++ dtc/dtc-lexer.l 2008-10-04 22:15:30.000000000 +1000
@@ -18,7 +18,7 @@
* USA
*/
-%option noyywrap nounput noinput yylineno
+%option noyywrap nounput noinput
%x INCLUDE
%x BYTESTRING
@@ -38,10 +38,11 @@ LINECOMMENT "//".*\n
#include "srcpos.h"
#include "dtc-parser.tab.h"
+
+/* CAUTION: this will stop working if we ever use yyless() or yyunput() */
#define YY_USER_ACTION \
{ \
- yylloc.file = current_srcfile; \
- yylloc.first_line = yylineno; \
+ srcpos_update(&yylloc, yytext, yyleng); \
}
/*#define LEXDEBUG 1*/
@@ -169,12 +170,9 @@ static void push_input_file(const char *
{
assert(filename);
- current_srcfile->lineno = yylineno;
-
srcfile_push(filename);
yyin = current_srcfile->f;
- yylineno = 1;
yypush_buffer_state(yy_create_buffer(yyin, YY_BUF_SIZE));
}
@@ -186,7 +184,6 @@ static int pop_input_file(void)
return 0;
yypop_buffer_state();
- yylineno = current_srcfile->lineno;
yyin = current_srcfile->f;
return 1;
Index: dtc/srcpos.h
===================================================================
--- dtc.orig/srcpos.h 2008-10-04 22:15:29.000000000 +1000
+++ dtc/srcpos.h 2008-10-04 22:22:14.000000000 +1000
@@ -31,7 +31,7 @@ struct srcfile_state {
FILE *f;
char *name;
char *dir;
- int lineno;
+ int lineno, colno;
struct srcfile_state *prev;
};
@@ -89,6 +89,7 @@ typedef YYLTYPE srcpos;
*/
extern srcpos srcpos_empty;
+extern void srcpos_update(srcpos *pos, const char *text, int len);
extern srcpos *srcpos_copy(srcpos *pos);
extern char *srcpos_string(srcpos *pos);
extern void srcpos_dump(srcpos *pos);
next prev parent reply other threads:[~2008-10-04 12:27 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-10-04 12:21 [0/5] Input file and srcpos rework (v2) David Gibson
[not found] ` <20081004122157.GT30184-787xzQ0H9iRg7VrjXcPTGA@public.gmane.org>
2008-10-04 12:27 ` [4/5] dtc: Cleanup srcpos_string() David Gibson
2008-10-04 12:27 ` David Gibson [this message]
2008-10-04 12:27 ` [5/5] dtc: Cleanup YYLTYPE and YYLLOC_DEFAULT declarations David Gibson
2008-10-04 12:27 ` [2/5] dtc: Simpler interface to source file management David Gibson
2008-10-04 12:27 ` [1/5] dtc: Move some functions to util.[ch] David Gibson
2008-10-08 19:52 ` [0/5] Input file and srcpos rework (v2) Jon Loeliger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20081004122706.5956EDDE11@ozlabs.org \
--to=david-xt8fgy+axnrb3ne2bgzf6laj5h9x9tb+@public.gmane.org \
--cc=devicetree-discuss-mnsaURCQ41sdnm+yROfE0A@public.gmane.org \
--cc=jdl-CYoMK+44s/E@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.