@@ -201,6 +201,7 @@ struct ParseState {
201
201
202
202
static Rboolean busy = FALSE;
203
203
static ParseState parseState ;
204
+ static char ParseErrorMsg [PARSE_ERROR_SIZE ];
204
205
205
206
#define PRESERVE_SV (x ) R_PreserveInMSet((x), parseState.mset)
206
207
#define RELEASE_SV (x ) R_ReleaseFromMSet((x), parseState.mset)
@@ -224,7 +225,7 @@ static int mkVerb2(const char *, int);
224
225
static int mkVerbEnv (void );
225
226
static int mkDollar (int );
226
227
227
- static SEXP R_LatexTagSymbol = NULL ;
228
+ static SEXP LatexTagSymbol = NULL ;
228
229
229
230
#define YYSTYPE SEXP
230
231
@@ -746,9 +747,9 @@ static const yytype_int8 yytranslate[] =
746
747
/* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
747
748
static const yytype_uint8 yyrline [] =
748
749
{
749
- 0 , 183 , 183 , 184 , 185 , 188 , 189 , 190 , 191 , 192 ,
750
- 193 , 195 , 196 , 198 , 199 , 200 , 201 , 202 , 203 , 204 ,
751
- 205 , 207 , 211 , 215 , 219 , 221 , 223 , 224
750
+ 0 , 184 , 184 , 185 , 186 , 189 , 190 , 191 , 192 , 193 ,
751
+ 194 , 196 , 197 , 199 , 200 , 201 , 202 , 203 , 204 , 205 ,
752
+ 206 , 208 , 212 , 216 , 220 , 222 , 224 , 225
752
753
};
753
754
#endif
754
755
@@ -2153,7 +2154,7 @@ static SEXP xxenv(SEXP begin, SEXP body, SEXP end, YYLTYPE *lloc)
2153
2154
RELEASE_SV (body );
2154
2155
}
2155
2156
setAttrib (ans , R_SrcrefSymbol , makeSrcref (lloc , parseState .SrcFile ));
2156
- setAttrib (ans , R_LatexTagSymbol , mkString ("ENVIRONMENT" ));
2157
+ setAttrib (ans , LatexTagSymbol , mkString ("ENVIRONMENT" ));
2157
2158
if (!isNull (end ))
2158
2159
RELEASE_SV (end );
2159
2160
#if DEBUGVALS
@@ -2171,7 +2172,7 @@ static SEXP xxmath(SEXP body, YYLTYPE *lloc, Rboolean display)
2171
2172
PRESERVE_SV (ans = PairToVectorList (CDR (body )));
2172
2173
RELEASE_SV (body );
2173
2174
setAttrib (ans , R_SrcrefSymbol , makeSrcref (lloc , parseState .SrcFile ));
2174
- setAttrib (ans , R_LatexTagSymbol ,
2175
+ setAttrib (ans , LatexTagSymbol ,
2175
2176
mkString (display ? "DISPLAYMATH" : "MATH" ));
2176
2177
#if DEBUGVALS
2177
2178
Rprintf (" result: %p\n" , ans );
@@ -2192,7 +2193,7 @@ static SEXP xxblock(SEXP body, YYLTYPE *lloc)
2192
2193
RELEASE_SV (body );
2193
2194
}
2194
2195
setAttrib (ans , R_SrcrefSymbol , makeSrcref (lloc , parseState .SrcFile ));
2195
- setAttrib (ans , R_LatexTagSymbol , mkString ("BLOCK" ));
2196
+ setAttrib (ans , LatexTagSymbol , mkString ("BLOCK" ));
2196
2197
2197
2198
#if DEBUGVALS
2198
2199
Rprintf (" result: %p\n" , ans );
@@ -2227,7 +2228,7 @@ static void xxsavevalue(SEXP items, YYLTYPE *lloc)
2227
2228
} else {
2228
2229
PRESERVE_SV (parseState .Value = allocVector (VECSXP , 1 ));
2229
2230
SET_VECTOR_ELT (parseState .Value , 0 , ScalarString (mkChar ("" )));
2230
- setAttrib (VECTOR_ELT (parseState .Value , 0 ), R_LatexTagSymbol , mkString ("TEXT" ));
2231
+ setAttrib (VECTOR_ELT (parseState .Value , 0 ), LatexTagSymbol , mkString ("TEXT" ));
2231
2232
}
2232
2233
if (!isNull (parseState .Value )) {
2233
2234
setAttrib (parseState .Value , R_ClassSymbol , mkString ("LaTeX" ));
@@ -2237,7 +2238,7 @@ static void xxsavevalue(SEXP items, YYLTYPE *lloc)
2237
2238
2238
2239
static SEXP xxtag (SEXP item , int type , YYLTYPE * lloc )
2239
2240
{
2240
- setAttrib (item , R_LatexTagSymbol , mkString (yytname [YYTRANSLATE (type )]));
2241
+ setAttrib (item , LatexTagSymbol , mkString (yytname [YYTRANSLATE (type )]));
2241
2242
setAttrib (item , R_SrcrefSymbol , makeSrcref (lloc , parseState .SrcFile ));
2242
2243
return item ;
2243
2244
}
@@ -2278,10 +2279,7 @@ static int xxgetc(void)
2278
2279
prevcols [prevpos ] = parseState .xxcolno ;
2279
2280
2280
2281
if (c == EOF ) return R_EOF ;
2281
-
2282
- R_ParseContextLast = (R_ParseContextLast + 1 ) % PARSE_CONTEXT_SIZE ;
2283
- R_ParseContext [R_ParseContextLast ] = (char ) c ;
2284
-
2282
+
2285
2283
if (c == '\n' ) {
2286
2284
parseState .xxlineno += 1 ;
2287
2285
parseState .xxcolno = 1 ;
@@ -2293,8 +2291,6 @@ static int xxgetc(void)
2293
2291
2294
2292
if (c == '\t' ) parseState .xxcolno = ((parseState .xxcolno + 6 ) & ~7 ) + 1 ;
2295
2293
2296
- R_ParseContextLine = parseState .xxlineno ;
2297
-
2298
2294
return c ;
2299
2295
}
2300
2296
@@ -2306,12 +2302,6 @@ static int xxungetc(int c)
2306
2302
parseState .xxcolno = prevcols [prevpos ];
2307
2303
prevpos = (prevpos + PUSHBACK_BUFSIZE - 1 ) % PUSHBACK_BUFSIZE ;
2308
2304
2309
- R_ParseContextLine = parseState .xxlineno ;
2310
-
2311
- R_ParseContext [R_ParseContextLast ] = '\0' ;
2312
- /* macOS requires us to keep this non-negative */
2313
- R_ParseContextLast = (R_ParseContextLast + PARSE_CONTEXT_SIZE - 1 )
2314
- % PARSE_CONTEXT_SIZE ;
2315
2305
if (npush >= PUSHBACK_BUFSIZE - 2 ) return R_EOF ;
2316
2306
pushback [npush ++ ] = c ;
2317
2307
return c ;
@@ -2402,18 +2392,9 @@ static void UseState(ParseState *state) {
2402
2392
parseState .prevState = state -> prevState ;
2403
2393
}
2404
2394
2405
- static void InitSymbols (void )
2406
- {
2407
- if (!R_LatexTagSymbol )
2408
- R_LatexTagSymbol = install ("latex_tag" );
2409
- }
2410
-
2411
2395
static SEXP ParseLatex (ParseStatus * status , SEXP srcfile )
2412
2396
{
2413
- InitSymbols ();
2414
-
2415
- R_ParseContextLast = 0 ;
2416
- R_ParseContext [0 ] = '\0' ;
2397
+ LatexTagSymbol = install ("latex_tag" );
2417
2398
2418
2399
parseState .xxInVerbEnv = NULL ;
2419
2400
@@ -2440,6 +2421,9 @@ static SEXP ParseLatex(ParseStatus *status, SEXP srcfile)
2440
2421
2441
2422
RELEASE_SV (parseState .Value );
2442
2423
UNPROTECT (1 ); /* parseState.mset */
2424
+
2425
+ if (* status == PARSE_ERROR )
2426
+ error ("%s" , ParseErrorMsg );
2443
2427
2444
2428
return parseState .Value ;
2445
2429
}
@@ -2459,14 +2443,6 @@ static int char_getc(void)
2459
2443
return (c );
2460
2444
}
2461
2445
2462
- static
2463
- SEXP R_ParseLatex (SEXP text , ParseStatus * status , SEXP srcfile )
2464
- {
2465
- nextchar_parse = translateCharUTF8 (STRING_ELT (text , 0 ));
2466
- ptr_getc = char_getc ;
2467
- return ParseLatex (status , srcfile );
2468
- }
2469
-
2470
2446
/*----------------------------------------------------------------------------
2471
2447
*
2472
2448
* The Lexical Analyzer:
@@ -2537,24 +2513,21 @@ static void yyerror(const char *s)
2537
2513
static char const yyshortunexpected [] = "unexpected %s" ;
2538
2514
static char const yylongunexpected [] = "unexpected %s '%s'" ;
2539
2515
char * expecting ;
2540
- char ParseErrorMsg [PARSE_ERROR_SIZE ];
2541
- SEXP filename ;
2542
- char ParseErrorFilename [PARSE_ERROR_SIZE ];
2543
-
2516
+ char ErrorTranslation [PARSE_ERROR_SIZE ];
2544
2517
if (!strncmp (s , yyunexpected , sizeof yyunexpected - 1 )) {
2545
2518
int i , translated = FALSE;
2546
2519
/* Edit the error message */
2547
2520
expecting = strstr (s + sizeof yyunexpected - 1 , yyexpecting );
2548
2521
if (expecting ) * expecting = '\0' ;
2549
2522
for (i = 0 ; yytname_translations [i ]; i += 2 ) {
2550
2523
if (!strcmp (s + sizeof yyunexpected - 1 , yytname_translations [i ])) {
2551
- if (yychar < 256 )
2552
- snprintf (ParseErrorMsg , PARSE_ERROR_SIZE ,
2524
+ if (yychar < 256 || yychar == END_OF_INPUT )
2525
+ snprintf (ErrorTranslation , sizeof ( ErrorTranslation ) ,
2553
2526
_ (yyshortunexpected ),
2554
2527
i /2 < YYENGLISH ? _ (yytname_translations [i + 1 ])
2555
2528
: yytname_translations [i + 1 ]);
2556
2529
else
2557
- snprintf (ParseErrorMsg , PARSE_ERROR_SIZE ,
2530
+ snprintf (ErrorTranslation , sizeof ( ErrorTranslation ) ,
2558
2531
_ (yylongunexpected ),
2559
2532
i /2 < YYENGLISH ? _ (yytname_translations [i + 1 ])
2560
2533
: yytname_translations [i + 1 ],
@@ -2564,48 +2537,45 @@ static void yyerror(const char *s)
2564
2537
}
2565
2538
}
2566
2539
if (!translated ) {
2567
- if (yychar < 256 )
2568
- snprintf (ParseErrorMsg , PARSE_ERROR_SIZE ,
2540
+ if (yychar < 256 || yychar == END_OF_INPUT )
2541
+ snprintf (ErrorTranslation , sizeof ( ErrorTranslation ) ,
2569
2542
_ (yyshortunexpected ),
2570
2543
s + sizeof yyunexpected - 1 );
2571
2544
else
2572
- snprintf (ParseErrorMsg , PARSE_ERROR_SIZE ,
2545
+ snprintf (ErrorTranslation , sizeof ( ErrorTranslation ) ,
2573
2546
_ (yylongunexpected ),
2574
2547
s + sizeof yyunexpected - 1 , CHAR (STRING_ELT (yylval , 0 )));
2575
2548
}
2576
2549
if (expecting ) {
2577
2550
translated = FALSE;
2578
2551
for (i = 0 ; yytname_translations [i ]; i += 2 ) {
2579
2552
if (!strcmp (expecting + sizeof yyexpecting - 1 , yytname_translations [i ])) {
2580
- strcat (ParseErrorMsg , _ (yyexpecting ));
2581
- strcat (ParseErrorMsg , i /2 < YYENGLISH ? _ (yytname_translations [i + 1 ])
2582
- : yytname_translations [i + 1 ]);
2553
+ strncat (ErrorTranslation , _ (yyexpecting ),
2554
+ sizeof (ErrorTranslation ) - strlen (ErrorTranslation ) - 1 );
2555
+ strncat (ErrorTranslation , i /2 < YYENGLISH
2556
+ ? _ (yytname_translations [i + 1 ])
2557
+ : yytname_translations [i + 1 ],
2558
+ sizeof (ErrorTranslation ) - strlen (ErrorTranslation ) - 1 );
2583
2559
translated = TRUE;
2584
2560
break ;
2585
2561
}
2586
2562
}
2587
2563
if (!translated ) {
2588
- strcat (ParseErrorMsg , _ (yyexpecting ));
2589
- strcat (ParseErrorMsg , expecting + sizeof yyexpecting - 1 );
2564
+ strncat (ErrorTranslation , _ (yyexpecting ),
2565
+ sizeof (ErrorTranslation ) - strlen (ErrorTranslation ) - 1 );
2566
+ strncat (ErrorTranslation , expecting + sizeof yyexpecting - 1 ,
2567
+ sizeof (ErrorTranslation ) - strlen (ErrorTranslation ) - 1 );
2590
2568
}
2591
2569
}
2592
2570
} else if (!strncmp (s , yyunknown , sizeof yyunknown - 1 )) {
2593
- snprintf (ParseErrorMsg , PARSE_ERROR_SIZE ,
2571
+ snprintf (ErrorTranslation , sizeof ( ErrorTranslation ) ,
2594
2572
"%s '%s'" , s , CHAR (STRING_ELT (yylval , 0 )));
2595
2573
} else {
2596
- snprintf (ParseErrorMsg , PARSE_ERROR_SIZE , "%s" , s );
2574
+ snprintf (ErrorTranslation , sizeof ( ErrorTranslation ), "%s" , s );
2597
2575
}
2598
- filename = findVar (install ("filename" ), parseState .SrcFile );
2599
- if (isString (filename ) && LENGTH (filename ))
2600
- strncpy (ParseErrorFilename , CHAR (STRING_ELT (filename , 0 )), PARSE_ERROR_SIZE - 1 );
2601
- else
2602
- ParseErrorFilename [0 ] = '\0' ;
2603
- if (yylloc .first_line != yylloc .last_line )
2604
- warning ("%s:%d-%d: %s" ,
2605
- ParseErrorFilename , yylloc .first_line , yylloc .last_line , ParseErrorMsg );
2606
- else
2607
- warning ("%s:%d: %s" ,
2608
- ParseErrorFilename , yylloc .first_line , ParseErrorMsg );
2576
+ snprintf (ParseErrorMsg , sizeof (ParseErrorMsg ),
2577
+ "Parse error at %d:%d: %s" , yylloc .first_line , yylloc .first_column ,
2578
+ ErrorTranslation );
2609
2579
}
2610
2580
2611
2581
#define TEXT_PUSH (c ) do { \
@@ -2877,7 +2847,7 @@ static void PopState(void) {
2877
2847
busy = FALSE;
2878
2848
}
2879
2849
2880
- /* "do_parseLatex "
2850
+ /* "parseLatex "
2881
2851
2882
2852
.External2("parseLatex", text, srcfile, verbose, verbatim, verb)
2883
2853
If there is text then that is read and the other arguments are ignored.
@@ -2894,8 +2864,7 @@ SEXP parseLatex(SEXP call, SEXP op, SEXP args, SEXP env)
2894
2864
yydebug = 1 ;
2895
2865
#endif
2896
2866
2897
- R_ParseError = 0 ;
2898
- R_ParseErrorMsg [0 ] = '\0' ;
2867
+ ParseErrorMsg [0 ] = '\0' ;
2899
2868
2900
2869
PushState ();
2901
2870
@@ -2908,10 +2877,13 @@ SEXP parseLatex(SEXP call, SEXP op, SEXP args, SEXP env)
2908
2877
parseState .xxVerbatimList = CAR (args ); args = CDR (args );
2909
2878
parseState .xxVerbList = CAR (args );
2910
2879
2911
- s = R_ParseLatex (text , & status , source );
2880
+ nextchar_parse = translateCharUTF8 (STRING_ELT (text , 0 ));
2881
+ ptr_getc = char_getc ;
2882
+ s = ParseLatex (& status , source );
2912
2883
2913
2884
PopState ();
2914
2885
2915
- if (status != PARSE_OK ) parseError (call , R_ParseError );
2886
+ if (status != PARSE_OK ) error ("%s" , ParseErrorMsg );
2887
+
2916
2888
return s ;
2917
2889
}
0 commit comments