# File name and line number of parser error/warning

**URL:** <https://www.ruby-forum.com/t/file-name-and-line-number-of-parser-error-warning/81704>\
**Category:** Development\
**Created:** [January 11, 2007, 4:05pm UTC](https://www.ruby-forum.com/t/file-name-and-line-number-of-parser-error-warning/81704 "2007-01-11T16:05:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 11, 2007, 4:05pm UTC](https://www.ruby-forum.com/t/file-name-and-line-number-of-parser-error-warning/81704/1 "2007-01-11T16:05:11Z")

</div>

e$B$J$+$@$G$9!#e(B

YARVe$B$K$J$C$F$+$i!"e(Bsyntax  
errore$B$de(Bparsee$B;~$N7Y9p$G%=!\<%9%U%!%$%kL\>e(B  
e$B$d9THV9f$,=P$J$/$J$C$F$$$^$9!#e(B

e$BEv=i!“e(Berr\_position()e$B$r\<B9T;~MQ$He(Bparsere$BMQ$KJ,$1$l$P$$$$$+$H;W$Ce(B  
e$B$?$s$G$9$,!”$I$&$;$J$ie(Bruby\_sourcefile/ruby\_sourcelinee$B$re(Brippere$BF1e(B  
e$BMMe(Bparser\_paramse$B$K0$7$Fe(Bparser\_warn()e$B$J$I$G=hM}$9$k$h$&$K$7$F$Oe(B  
e$B$I$&$+$H$$$&5$$b$7$^$9!#e(B

e$B$I$A$i$K$9$k$K$7$F$b!"%7%0%J%k$G8F$P$l$k2DG=@-$N$"$ke(Brb\_bug()e$B$Oe(B  
e$BC1=c$K$I$A$i$+0lJ}$K$O$G$-$=$&$K$J$$$H$$$&LdBj$,$"$j$^$9!#$I$&e(B  
e$B$7$?$b$s$G$7$g$&$+!#e(B

e$B0l1~!":G=i$N0F$G;n$7$F$\_$?%Q%C%A$r$D$1$F$\*$-$^$9!#e(B

# Index: parse.y

— parse.y (revision 11525)  
+++ parse.y (working copy)  
@@ -500,9 +500,9 @@ static VALUE ripper\_id2sym(ID);

#ifndef RIPPER  
-# define rb\_warn0(fmt) rb\_warn(fmt)  
-# define rb\_warnI(fmt,a) rb\_warn(fmt,a)  
-# define rb\_warnS(fmt,a) rb\_warn(fmt,a)  
-# define rb\_warning0(fmt) rb\_warning(fmt)  
-# define rb\_warningS(fmt,a) rb\_warning(fmt,a)  
+# define rb\_warn0(fmt) rb\_compile\_warn(fmt)  
+# define rb\_warnI(fmt,a) rb\_compile\_warn(fmt,a)  
+# define rb\_warnS(fmt,a) rb\_compile\_warn(fmt,a)  
+# define rb\_warning0(fmt) rb\_compile\_warning(fmt)  
+# define rb\_warningS(fmt,a) rb\_compile\_warning(fmt,a)  
#else

# define rb\_warn0(fmt) ripper\_warn0(parser, fmt)

@@ -739,5 +739,5 @@ bodystmt : compstmt  
}  
else if ($3) {

- 

```
     rb_warn("else without rescue is useless");

```

- 

```
     rb_warn0("else without rescue is useless");
     $$ = block_append($$, $3);
 }

```

@@ -2234,5 +2234,5 @@ opt\_call\_args : none  
call\_args : command  
{

- 

```
       rb_warn("parenthesize argument(s) for future version");

```

- 

```
       rb_warn0("parenthesize argument(s) for future version");
   /*%%%*/
 $$ = NEW_LIST($1);

```

@@ -6987,5 +6987,5 @@ parser\_warn(NODE \*node, const char \*mesg  
int line = ruby\_sourceline;  
ruby\_sourceline = nd\_line(node);

- rb\_warn("%s", mesg);

- rb\_warnS("%s", mesg);  
ruby\_sourceline = line;  
}  
@@ -7593,5 +7593,5 @@ void\_expr\_gen(struct parser\_params \*pars

ruby\_sourceline = nd\_line(node);

- rb\_warn(“useless use of %s in void context”, useless);

- rb\_warnS(“useless use of %s in void context”, useless);  
ruby\_sourceline = line;  
}  
@@ -7788,5 +7788,5 @@ cond0(struct parser\_params \*parser, NODE  
case NODE\_EVSTR:  
case NODE\_STR:

- rb\_warn(“string literal in condition”);

- rb\_warn0(“string literal in condition”);  
break;

# Index: error.c

— error.c (revision 11525)  
+++ error.c (working copy)  
@@ -31,18 +31,30 @@ int rb\_sourceline();

static int  
-err\_position(char \*buf, long len)  
+err\_position\_0(char \*buf, long len, const char \*file, long line)  
{

- ruby\_set\_current\_source();
- if (!rb\_sourcefile()) {

- if (!file) {  
return 0;  
}

- else if (rb\_sourceline() == 0) {
- return snprintf(buf, len, "%s: ", rb\_sourcefile());

- else if (line == 0) {
- return snprintf(buf, len, "%s: ", file);  
}  
else {

- return snprintf(buf, len, "%s:%d: ", rb\_sourcefile(),  
rb\_sourceline());

- return snprintf(buf, len, "%s:%d: ", file, line);  
}  
}

+static int  
+err\_position(char \*buf, long len)  
+{

- return err\_position\_0(buf, len, rb\_sourcefile(), rb\_sourceline());  
+}
- 

+static int  
+compile\_position(char \*buf, long len)  
+{

- ruby\_set\_current\_source();
- return err\_position\_0(buf, len, ruby\_sourcefile, ruby\_sourceline);  
+}
- 

static void  
err\_snprintf(char \*buf, long len, const char \*fmt, va\_list args)  
@@ -56,21 +68,27 @@ err\_snprintf(char \*buf, long len, const  
}

-static void err\_append(const char\*);  
static void  
-err\_print(const char \*fmt, va\_list args)  
+compile\_snprintf(char \*buf, long len, const char \*fmt, va\_list args)  
{

- char buf[BUFSIZ];

- long n;

- err\_snprintf(buf, BUFSIZ, fmt, args);
- err\_append(buf);

- n = compile\_position(buf, len);
- if (len \> n) {
- vsnprintf((char\*)buf+n, len-n, fmt, args);
- }  
}

+static void err\_append(const char\*);  
+  
void  
rb\_compile\_error(const char \*fmt, …)  
{  
va\_list args;

- char buf[BUFSIZ];
- va\_start(args, fmt);

- err\_print(fmt, args);

- compile\_snprintf(buf, BUFSIZ, fmt, args);  
va\_end(args);
- err\_append(buf);  
ruby\_nerrs++;  
}  
@@ -89,4 +107,47 @@ rb\_compile\_error\_append(const char \*fmt,

static void  
+compile\_warn\_print(const char \*fmt, va\_list args)  
+{

- char buf[BUFSIZ];
- int len;
- 
- compile\_snprintf(buf, BUFSIZ, fmt, args);
- len = strlen(buf);
- buf[len++] = ‘\n’;
- rb\_write\_error2(buf, len);  
+}
- 

+void  
+rb\_compile\_warn(const char \*fmt, …)  
+{

- char buf[BUFSIZ];
- va\_list args;
- 
- if (NIL\_P(ruby\_verbose)) return;
- 
- snprintf(buf, BUFSIZ, “warning: %s”, fmt);
- 
- va\_start(args, fmt);
- compile\_warn\_print(buf, args);
- va\_end(args);  
+}
- 

+/\* rb\_compile\_warning() reports only in verbose mode \*/  
+void  
+rb\_compile\_warning(const char \*fmt, …)  
+{

- char buf[BUFSIZ];
- va\_list args;
- 
- if (!RTEST(ruby\_verbose)) return;
- 
- snprintf(buf, BUFSIZ, “warning: %s”, fmt);
- 
- va\_start(args, fmt);
- compile\_warn\_print(buf, args);
- va\_end(args);  
+}
- 

# +static void warn\_print(const char \*fmt, va\_list args) { Index: ruby.h

— ruby.h (revision 11525)  
+++ ruby.h (working copy)  
@@ -632,8 +632,10 @@ NORETURN(void rb\_notimplement(void));  
/\* reports if `-w' specified */ PRINTF_ARGS(void rb_warning(const char*, ...), 1, 2); +PRINTF_ARGS(void rb_compile_warning(const char*, ...), 1, 2); /* reports if`-w’ specified _/  
PRINTF\_ARGS(void rb\_sys\_warning(const char_, …), 1, 2);  
/\* reports always _/  
PRINTF\_ARGS(void rb\_warn(const char_, …), 1, 2);  
+PRINTF\_ARGS(void rb\_compile\_warn(const char\*, …), 1, 2);

VALUE rb\_each(VALUE);

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 23, 2007, 4:18am UTC](https://www.ruby-forum.com/t/file-name-and-line-number-of-parser-error-warning/81704/2 "2007-01-23T04:18:43Z")

</div>

e$B!!$5$5$@$G$9!#e(B

e$B!!JV;v$,CY$/$J$C$F$9$\_$^$;$s!#e(B

removed\_email\_address@domain.invalid wrote:

> e$B$7$?$b$s$G$7$g$&$+!#e(B
> 
> e$B0l1~!":G=i$N0F$G;n$7$F$\_$?%Q%C%A$r$D$1$F$\*$-$^$9!#e(B

e$B!!8e\<T$N0F$,$$$$!"$H$$$&$+!"%0%m!\<%P%kJQ?te(B ruby\_sourcefile/line  
e$B$O\>C5n$7e(B  
e$B$?$$$N$G$9$,!"Fq$7$$$G$7$g$&$+!#e(B
