PageRenderTime 77ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/R-2.15.1/src/main/gramLatex.y

#
Happy | 873 lines | 737 code | 136 blank | 0 comment | 0 complexity | a639641d995f5908b83a3eefebe4155a MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, CC-BY-SA-4.0, BSD-3-Clause, AGPL-3.0, GPL-2.0, GPL-3.0, LGPL-2.0
  1. %{
  2. /*
  3. * R : A Computer Langage for Statistical Data Analysis
  4. * Copyright (C) 1995, 1996, 1997 Robert Gentleman and Ross Ihaka
  5. * Copyright (C) 1997--2010 The R Core Team
  6. * Copyright (C) 2010 Duncan Murdoch
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, a copy is available at
  20. * http://www.r-project.org/Licenses/
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <Defn.h>
  26. #include <Parse.h>
  27. #define STRICT_R_HEADERS
  28. #include <R_ext/RS.h> /* for R_chk_* allocation */
  29. #include <ctype.h>
  30. /* bison creates a non-static symbol yylloc in both gramLatex.o and gramRd.o,
  31. so remap */
  32. #define yylloc yyllocL
  33. #define DEBUGVALS 0 /* 1 causes detailed internal state output to R console */
  34. #define DEBUGMODE 0 /* 1 causes Bison output of parse state, to stdout or stderr */
  35. #define YYERROR_VERBOSE 1
  36. static void yyerror(const char *);
  37. static int yylex();
  38. static int yyparse(void);
  39. #define yyconst const
  40. typedef struct yyltype
  41. {
  42. int first_line;
  43. int first_column;
  44. int first_byte;
  45. int last_line;
  46. int last_column;
  47. int last_byte;
  48. } yyltype;
  49. # define YYLTYPE yyltype
  50. # define YYLLOC_DEFAULT(Current, Rhs, N) \
  51. do \
  52. if (YYID (N)) \
  53. { \
  54. (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
  55. (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
  56. (Current).first_byte = YYRHSLOC (Rhs, 1).first_byte; \
  57. (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
  58. (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
  59. (Current).last_byte = YYRHSLOC (Rhs, N).last_byte; \
  60. } \
  61. else \
  62. { \
  63. (Current).first_line = (Current).last_line = \
  64. YYRHSLOC (Rhs, 0).last_line; \
  65. (Current).first_column = (Current).last_column = \
  66. YYRHSLOC (Rhs, 0).last_column; \
  67. (Current).first_byte = (Current).last_byte = \
  68. YYRHSLOC (Rhs, 0).last_byte; \
  69. } \
  70. while (YYID (0))
  71. /* Useful defines so editors don't get confused ... */
  72. #define LBRACE '{'
  73. #define RBRACE '}'
  74. /* Functions used in the parsing process */
  75. static SEXP GrowList(SEXP, SEXP);
  76. static int KeywordLookup(const char *);
  77. static SEXP NewList(void);
  78. static SEXP makeSrcref(YYLTYPE *, SEXP);
  79. /* Internal lexer / parser state variables */
  80. static int xxgetc();
  81. static int xxungetc(int);
  82. static int xxlineno, xxbyteno, xxcolno;
  83. static int xxDebugTokens; /* non-zero causes debug output to R console */
  84. static SEXP Value;
  85. static int xxinitvalue;
  86. static char const yyunknown[] = "unknown macro"; /* our message, not bison's */
  87. static SEXP xxInVerbEnv; /* Are we currently in a verbatim environment? If
  88. so, this is the string to end it. If not,
  89. this is NULL */
  90. static SEXP xxVerbatimList;/* A STRSXP containing all the verbatim environment names */
  91. static SEXP SrcFile; /* parseLatex will *always* supply a srcfile */
  92. /* Routines used to build the parse tree */
  93. static SEXP xxnewlist(SEXP);
  94. static SEXP xxlist(SEXP, SEXP);
  95. static void xxsavevalue(SEXP, YYLTYPE *);
  96. static SEXP xxtag(SEXP, int, YYLTYPE *);
  97. static SEXP xxenv(SEXP, SEXP, SEXP, YYLTYPE *);
  98. static SEXP xxmath(SEXP, YYLTYPE *);
  99. static SEXP xxblock(SEXP, YYLTYPE *);
  100. static void xxSetInVerbEnv(SEXP);
  101. static int mkMarkup(int);
  102. static int mkText(int);
  103. static int mkComment(int);
  104. static int mkVerb(int);
  105. static int mkVerbEnv();
  106. #define YYSTYPE SEXP
  107. %}
  108. %debug
  109. %token END_OF_INPUT ERROR
  110. %token MACRO
  111. %token TEXT COMMENT
  112. %token BEGIN END VERB
  113. /* Recent bison has <> to represent all of the destructors below, but we don't assume it */
  114. /* I think we need to list everything here which occurs before the last item in a
  115. pattern, just in case the last item is unmatched and we need to back out. But
  116. it is safe to list more, so we do. */
  117. %destructor { UNPROTECT_PTR($$); } MACRO TEXT COMMENT BEGIN END
  118. %%
  119. Init: Items END_OF_INPUT { xxsavevalue($1, &@$); return 0; }
  120. | END_OF_INPUT { xxsavevalue(NULL, &@$); return 0; }
  121. | error { PROTECT(Value = R_NilValue); YYABORT; }
  122. ;
  123. Items: Item { $$ = xxnewlist($1); }
  124. | math { $$ = xxnewlist($1); }
  125. | Items Item { $$ = xxlist($1, $2); }
  126. | Items math { $$ = xxlist($1, $2); }
  127. nonMath: Item { $$ = xxnewlist($1); }
  128. | nonMath Item { $$ = xxlist($1, $2); }
  129. Item: TEXT { $$ = xxtag($1, TEXT, &@$); }
  130. | COMMENT { $$ = xxtag($1, COMMENT, &@$); }
  131. | MACRO { $$ = xxtag($1, MACRO, &@$); }
  132. | VERB { $$ = xxtag($1, VERB, &@$); }
  133. | environment { $$ = $1; }
  134. | block { $$ = $1; }
  135. environment: BEGIN '{' TEXT '}' { xxSetInVerbEnv($3); }
  136. Items END '{' TEXT '}' { $$ = xxenv($3, $6, $9, &@$);
  137. UNPROTECT_PTR($1); UNPROTECT_PTR($7); }
  138. math: '$' nonMath '$' { $$ = xxmath($2, &@$); }
  139. block: '{' Items '}' { $$ = xxblock($2, &@$); }
  140. | '{' '}' { $$ = xxblock(NULL, &@$); }
  141. %%
  142. static SEXP xxnewlist(SEXP item)
  143. {
  144. SEXP ans, tmp;
  145. #if DEBUGVALS
  146. Rprintf("xxnewlist(item=%p)", item);
  147. #endif
  148. PROTECT(tmp = NewList());
  149. if (item) {
  150. PROTECT(ans = GrowList(tmp, item));
  151. UNPROTECT_PTR(tmp);
  152. UNPROTECT_PTR(item);
  153. } else ans = tmp;
  154. #if DEBUGVALS
  155. Rprintf(" result: %p is length %d\n", ans, length(ans));
  156. #endif
  157. return ans;
  158. }
  159. static SEXP xxlist(SEXP oldlist, SEXP item)
  160. {
  161. SEXP ans;
  162. #if DEBUGVALS
  163. Rprintf("xxlist(oldlist=%p, item=%p)", oldlist, item);
  164. #endif
  165. PROTECT(ans = GrowList(oldlist, item));
  166. UNPROTECT_PTR(item);
  167. UNPROTECT_PTR(oldlist);
  168. #if DEBUGVALS
  169. Rprintf(" result: %p is length %d\n", ans, length(ans));
  170. #endif
  171. return ans;
  172. }
  173. static SEXP xxenv(SEXP begin, SEXP body, SEXP end, YYLTYPE *lloc)
  174. {
  175. SEXP ans;
  176. #if DEBUGVALS
  177. Rprintf("xxenv(begin=%p, body=%p, end=%p)", begin, body, end);
  178. #endif
  179. PROTECT(ans = allocVector(VECSXP, 2));
  180. SET_VECTOR_ELT(ans, 0, begin);
  181. UNPROTECT_PTR(begin);
  182. if (!isNull(body)) {
  183. SET_VECTOR_ELT(ans, 1, PairToVectorList(CDR(body)));
  184. UNPROTECT_PTR(body);
  185. }
  186. /* FIXME: check that begin and end match */
  187. setAttrib(ans, R_SrcrefSymbol, makeSrcref(lloc, SrcFile));
  188. setAttrib(ans, install("latex_tag"), mkString("ENVIRONMENT"));
  189. if (!isNull(end))
  190. UNPROTECT_PTR(end);
  191. #if DEBUGVALS
  192. Rprintf(" result: %p\n", ans);
  193. #endif
  194. return ans;
  195. }
  196. static SEXP xxmath(SEXP body, YYLTYPE *lloc)
  197. {
  198. SEXP ans;
  199. #if DEBUGVALS
  200. Rprintf("xxmath(body=%p)", body);
  201. #endif
  202. PROTECT(ans = PairToVectorList(CDR(body)));
  203. UNPROTECT_PTR(body);
  204. setAttrib(ans, R_SrcrefSymbol, makeSrcref(lloc, SrcFile));
  205. setAttrib(ans, install("latex_tag"), mkString("MATH"));
  206. #if DEBUGVALS
  207. Rprintf(" result: %p\n", ans);
  208. #endif
  209. return ans;
  210. }
  211. static SEXP xxblock(SEXP body, YYLTYPE *lloc)
  212. {
  213. SEXP ans;
  214. #if DEBUGVALS
  215. Rprintf("xxblock(body=%p)", body);
  216. #endif
  217. if (!body)
  218. PROTECT(ans = allocVector(VECSXP, 0));
  219. else {
  220. PROTECT(ans = PairToVectorList(CDR(body)));
  221. UNPROTECT_PTR(body);
  222. }
  223. setAttrib(ans, R_SrcrefSymbol, makeSrcref(lloc, SrcFile));
  224. setAttrib(ans, install("latex_tag"), mkString("BLOCK"));
  225. #if DEBUGVALS
  226. Rprintf(" result: %p\n", ans);
  227. #endif
  228. return ans;
  229. }
  230. static int VerbatimLookup(const char *s)
  231. {
  232. int i;
  233. for (i = 0; i < length(xxVerbatimList); i++) {
  234. if (strcmp(s, CHAR(STRING_ELT(xxVerbatimList, i))) == 0)
  235. return TRUE;
  236. }
  237. return FALSE;
  238. }
  239. static void xxSetInVerbEnv(SEXP envname)
  240. {
  241. char buffer[256];
  242. if (VerbatimLookup(CHAR(STRING_ELT(envname, 0)))) {
  243. snprintf(buffer, sizeof(buffer), "\\end{%s}", CHAR(STRING_ELT(envname, 0)));
  244. PROTECT(xxInVerbEnv = ScalarString(mkChar(buffer)));
  245. } else xxInVerbEnv = NULL;
  246. }
  247. static void xxsavevalue(SEXP items, YYLTYPE *lloc)
  248. {
  249. if (items) {
  250. PROTECT(Value = PairToVectorList(CDR(items)));
  251. UNPROTECT_PTR(items);
  252. } else {
  253. PROTECT(Value = allocVector(VECSXP, 1));
  254. SET_VECTOR_ELT(Value, 0, ScalarString(mkChar("")));
  255. setAttrib(VECTOR_ELT(Value, 0), install("latex_tag"), mkString("TEXT"));
  256. }
  257. if (!isNull(Value)) {
  258. setAttrib(Value, R_ClassSymbol, mkString("LaTeX"));
  259. setAttrib(Value, R_SrcrefSymbol, makeSrcref(lloc, SrcFile));
  260. }
  261. }
  262. static SEXP xxtag(SEXP item, int type, YYLTYPE *lloc)
  263. {
  264. setAttrib(item, install("latex_tag"), mkString(yytname[YYTRANSLATE(type)]));
  265. setAttrib(item, R_SrcrefSymbol, makeSrcref(lloc, SrcFile));
  266. return item;
  267. }
  268. /*----------------------------------------------------------------------------*/
  269. static int (*ptr_getc)(void);
  270. /* Private pushback, since file ungetc only guarantees one byte.
  271. We need up to one MBCS-worth */
  272. #define PUSHBACK_BUFSIZE 30
  273. static int pushback[PUSHBACK_BUFSIZE];
  274. static unsigned int npush = 0;
  275. static int prevpos = 0;
  276. static int prevlines[PUSHBACK_BUFSIZE];
  277. static int prevcols[PUSHBACK_BUFSIZE];
  278. static int prevbytes[PUSHBACK_BUFSIZE];
  279. static int xxgetc(void)
  280. {
  281. int c, oldpos;
  282. if(npush) c = pushback[--npush]; else c = ptr_getc();
  283. oldpos = prevpos;
  284. prevpos = (prevpos + 1) % PUSHBACK_BUFSIZE;
  285. prevbytes[prevpos] = xxbyteno;
  286. prevlines[prevpos] = xxlineno;
  287. /* We only advance the column for the 1st byte in UTF-8, so handle later bytes specially */
  288. if (0x80 <= (unsigned char)c && (unsigned char)c <= 0xBF) {
  289. xxcolno--;
  290. prevcols[prevpos] = prevcols[oldpos];
  291. } else
  292. prevcols[prevpos] = xxcolno;
  293. if (c == EOF) return R_EOF;
  294. R_ParseContextLast = (R_ParseContextLast + 1) % PARSE_CONTEXT_SIZE;
  295. R_ParseContext[R_ParseContextLast] = c;
  296. if (c == '\n') {
  297. xxlineno += 1;
  298. xxcolno = 1;
  299. xxbyteno = 1;
  300. } else {
  301. xxcolno++;
  302. xxbyteno++;
  303. }
  304. if (c == '\t') xxcolno = ((xxcolno + 6) & ~7) + 1;
  305. R_ParseContextLine = xxlineno;
  306. return c;
  307. }
  308. static int xxungetc(int c)
  309. {
  310. /* this assumes that c was the result of xxgetc; if not, some edits will be needed */
  311. xxlineno = prevlines[prevpos];
  312. xxbyteno = prevbytes[prevpos];
  313. xxcolno = prevcols[prevpos];
  314. prevpos = (prevpos + PUSHBACK_BUFSIZE - 1) % PUSHBACK_BUFSIZE;
  315. R_ParseContextLine = xxlineno;
  316. R_ParseContext[R_ParseContextLast] = '\0';
  317. /* Mac OS X requires us to keep this non-negative */
  318. R_ParseContextLast = (R_ParseContextLast + PARSE_CONTEXT_SIZE - 1)
  319. % PARSE_CONTEXT_SIZE;
  320. if(npush >= PUSHBACK_BUFSIZE - 2) return R_EOF;
  321. pushback[npush++] = c;
  322. return c;
  323. }
  324. static SEXP makeSrcref(YYLTYPE *lloc, SEXP srcfile)
  325. {
  326. SEXP val;
  327. PROTECT(val = allocVector(INTSXP, 6));
  328. INTEGER(val)[0] = lloc->first_line;
  329. INTEGER(val)[1] = lloc->first_byte;
  330. INTEGER(val)[2] = lloc->last_line;
  331. INTEGER(val)[3] = lloc->last_byte;
  332. INTEGER(val)[4] = lloc->first_column;
  333. INTEGER(val)[5] = lloc->last_column;
  334. setAttrib(val, R_SrcfileSymbol, srcfile);
  335. setAttrib(val, R_ClassSymbol, mkString("srcref"));
  336. UNPROTECT(1);
  337. return val;
  338. }
  339. static SEXP mkString2(const char *s, int len)
  340. {
  341. SEXP t;
  342. cetype_t enc = CE_UTF8;
  343. PROTECT(t = allocVector(STRSXP, 1));
  344. SET_STRING_ELT(t, 0, mkCharLenCE(s, len, enc));
  345. UNPROTECT(1);
  346. return t;
  347. }
  348. /* Stretchy List Structures : Lists are created and grown using a special */
  349. /* dotted pair. The CAR of the list points to the last cons-cell in the */
  350. /* list and the CDR points to the first. The list can be extracted from */
  351. /* the pair by taking its CDR, while the CAR gives fast access to the end */
  352. /* of the list. */
  353. /* Create a stretchy-list dotted pair */
  354. static SEXP NewList(void)
  355. {
  356. SEXP s = CONS(R_NilValue, R_NilValue);
  357. SETCAR(s, s);
  358. return s;
  359. }
  360. /* Add a new element at the end of a stretchy list */
  361. static SEXP GrowList(SEXP l, SEXP s)
  362. {
  363. SEXP tmp;
  364. PROTECT(s);
  365. tmp = CONS(s, R_NilValue);
  366. UNPROTECT(1);
  367. SETCDR(CAR(l), tmp);
  368. SETCAR(l, tmp);
  369. return l;
  370. }
  371. /*--------------------------------------------------------------------------*/
  372. /*
  373. * Parsing Entry Points:
  374. *
  375. * The Following entry points provide Rd parsing facilities.
  376. *
  377. * SEXP R_ParseLatex(SEXP text, ParseStatus *status, SEXP srcfile)
  378. *
  379. */
  380. static SEXP ParseLatex(ParseStatus *status, SEXP srcfile)
  381. {
  382. R_ParseContextLast = 0;
  383. R_ParseContext[0] = '\0';
  384. xxInVerbEnv = NULL;
  385. xxlineno = 1;
  386. xxcolno = 1;
  387. xxbyteno = 1;
  388. SrcFile = srcfile;
  389. npush = 0;
  390. Value = R_NilValue;
  391. if (yyparse()) *status = PARSE_ERROR;
  392. else *status = PARSE_OK;
  393. #if DEBUGVALS
  394. Rprintf("ParseRd result: %p\n", Value);
  395. #endif
  396. UNPROTECT_PTR(Value);
  397. return Value;
  398. }
  399. static const char * nextchar_parse;
  400. /* need to handle incomplete last line */
  401. static int char_getc(void)
  402. {
  403. int c;
  404. c = *nextchar_parse++;
  405. if (!c) {
  406. c = R_EOF;
  407. nextchar_parse--;
  408. }
  409. return (c);
  410. }
  411. attribute_hidden
  412. SEXP R_ParseLatex(SEXP text, ParseStatus *status, SEXP srcfile)
  413. {
  414. nextchar_parse = CHAR(STRING_ELT(text, 0));
  415. ptr_getc = char_getc;
  416. return ParseLatex(status, srcfile);
  417. }
  418. /*----------------------------------------------------------------------------
  419. *
  420. * The Lexical Analyzer:
  421. *
  422. * Basic lexical analysis is performed by the following
  423. * routines.
  424. *
  425. * The function yylex() scans the input, breaking it into
  426. * tokens which are then passed to the parser.
  427. *
  428. */
  429. /* Special Symbols */
  430. /* Section and R code headers */
  431. struct {
  432. char *name;
  433. int token;
  434. }
  435. static keywords[] = {
  436. /* These sections contain Latex-like text */
  437. { "\\begin", BEGIN },
  438. { "\\end", END },
  439. { "\\verb", VERB },
  440. { 0, 0 }
  441. /* All other markup macros are rejected. */
  442. };
  443. /* Record the longest # directive here */
  444. #define DIRECTIVE_LEN 7
  445. static int KeywordLookup(const char *s)
  446. {
  447. int i;
  448. for (i = 0; keywords[i].name; i++) {
  449. if (strcmp(keywords[i].name, s) == 0)
  450. return keywords[i].token;
  451. }
  452. return MACRO;
  453. }
  454. static void yyerror(const char *s)
  455. {
  456. static const char *const yytname_translations[] =
  457. {
  458. /* the left column are strings coming from bison, the right
  459. column are translations for users.
  460. The first YYENGLISH from the right column are English to be translated,
  461. the rest are to be copied literally. The #if 0 block below allows xgettext
  462. to see these.
  463. */
  464. #define YYENGLISH 3
  465. "$undefined", "input",
  466. "LATEXMACRO", "macro",
  467. "ESCAPE", "macro",
  468. 0, 0
  469. };
  470. static char const yyunexpected[] = "syntax error, unexpected ";
  471. static char const yyexpecting[] = ", expecting ";
  472. static char const yyshortunexpected[] = "unexpected %s";
  473. static char const yylongunexpected[] = "unexpected %s '%s'";
  474. char *expecting;
  475. char ParseErrorMsg[PARSE_ERROR_SIZE];
  476. SEXP filename;
  477. char ParseErrorFilename[PARSE_ERROR_SIZE];
  478. #if 0
  479. /* these are just here to trigger the internationalization */
  480. _("input");
  481. _("macro");
  482. #endif
  483. if (!strncmp(s, yyunexpected, sizeof yyunexpected -1)) {
  484. int i, translated = FALSE;
  485. /* Edit the error message */
  486. expecting = strstr(s + sizeof yyunexpected -1, yyexpecting);
  487. if (expecting) *expecting = '\0';
  488. for (i = 0; yytname_translations[i]; i += 2) {
  489. if (!strcmp(s + sizeof yyunexpected - 1, yytname_translations[i])) {
  490. if (yychar < 256)
  491. sprintf(ParseErrorMsg, _(yyshortunexpected),
  492. i/2 < YYENGLISH ? _(yytname_translations[i+1])
  493. : yytname_translations[i+1]);
  494. else
  495. sprintf(ParseErrorMsg, _(yylongunexpected),
  496. i/2 < YYENGLISH ? _(yytname_translations[i+1])
  497. : yytname_translations[i+1],
  498. CHAR(STRING_ELT(yylval, 0)));
  499. translated = TRUE;
  500. break;
  501. }
  502. }
  503. if (!translated) {
  504. if (yychar < 256)
  505. sprintf(ParseErrorMsg, _(yyshortunexpected),
  506. s + sizeof yyunexpected - 1);
  507. else
  508. sprintf(ParseErrorMsg, _(yylongunexpected),
  509. s + sizeof yyunexpected - 1, CHAR(STRING_ELT(yylval, 0)));
  510. }
  511. if (expecting) {
  512. translated = FALSE;
  513. for (i = 0; yytname_translations[i]; i += 2) {
  514. if (!strcmp(expecting + sizeof yyexpecting - 1, yytname_translations[i])) {
  515. strcat(ParseErrorMsg, _(yyexpecting));
  516. strcat(ParseErrorMsg, i/2 < YYENGLISH ? _(yytname_translations[i+1])
  517. : yytname_translations[i+1]);
  518. translated = TRUE;
  519. break;
  520. }
  521. }
  522. if (!translated) {
  523. strcat(ParseErrorMsg, _(yyexpecting));
  524. strcat(ParseErrorMsg, expecting + sizeof yyexpecting - 1);
  525. }
  526. }
  527. } else if (!strncmp(s, yyunknown, sizeof yyunknown-1)) {
  528. sprintf(ParseErrorMsg, "%s '%s'", s, CHAR(STRING_ELT(yylval, 0)));
  529. } else {
  530. sprintf(ParseErrorMsg, "%s", s);
  531. }
  532. filename = findVar(install("filename"), SrcFile);
  533. if (isString(filename) && length(filename))
  534. strncpy(ParseErrorFilename, CHAR(STRING_ELT(filename, 0)), PARSE_ERROR_SIZE - 1);
  535. else
  536. ParseErrorFilename[0] = '\0';
  537. if (yylloc.first_line != yylloc.last_line)
  538. warning("%s:%d-%d: %s",
  539. ParseErrorFilename, yylloc.first_line, yylloc.last_line, ParseErrorMsg);
  540. else
  541. warning("%s:%d: %s",
  542. ParseErrorFilename, yylloc.first_line, ParseErrorMsg);
  543. }
  544. #define TEXT_PUSH(c) do { \
  545. unsigned int nc = bp - stext; \
  546. if (nc >= nstext - 1) { \
  547. char *old = stext; \
  548. nstext *= 2; \
  549. stext = malloc(nstext); \
  550. if(!stext) error(_("unable to allocate buffer for long string at line %d"), xxlineno);\
  551. memmove(stext, old, nc); \
  552. if(old != st0) free(old); \
  553. bp = stext+nc; } \
  554. *bp++ = (c); \
  555. } while(0)
  556. static void setfirstloc(void)
  557. {
  558. yylloc.first_line = xxlineno;
  559. yylloc.first_column = xxcolno;
  560. yylloc.first_byte = xxbyteno;
  561. }
  562. static void setlastloc(void)
  563. {
  564. yylloc.last_line = prevlines[prevpos];
  565. yylloc.last_column = prevcols[prevpos];
  566. yylloc.last_byte = prevbytes[prevpos];
  567. }
  568. /* Split the input stream into tokens. */
  569. /* This is the lowest of the parsing levels. */
  570. static int token(void)
  571. {
  572. int c;
  573. if (xxinitvalue) {
  574. yylloc.first_line = 0;
  575. yylloc.first_column = 0;
  576. yylloc.first_byte = 0;
  577. yylloc.last_line = 0;
  578. yylloc.last_column = 0;
  579. yylloc.last_byte = 0;
  580. PROTECT(yylval = mkString(""));
  581. c = xxinitvalue;
  582. xxinitvalue = 0;
  583. return(c);
  584. }
  585. setfirstloc();
  586. if (xxInVerbEnv)
  587. return mkVerbEnv();
  588. c = xxgetc();
  589. switch (c) {
  590. case '%': return mkComment(c);
  591. case '\\':return mkMarkup(c);
  592. case R_EOF:return END_OF_INPUT;
  593. case LBRACE:return c;
  594. case RBRACE:return c;
  595. case '$': return c;
  596. }
  597. return mkText(c);
  598. }
  599. #define INITBUFSIZE 128
  600. static int mkText(int c)
  601. {
  602. char st0[INITBUFSIZE];
  603. unsigned int nstext = INITBUFSIZE;
  604. char *stext = st0, *bp = st0;
  605. while(1) {
  606. switch (c) {
  607. case '\\':
  608. case '%':
  609. case LBRACE:
  610. case RBRACE:
  611. case '$':
  612. case R_EOF:
  613. goto stop;
  614. }
  615. TEXT_PUSH(c);
  616. c = xxgetc();
  617. };
  618. stop:
  619. xxungetc(c);
  620. PROTECT(yylval = mkString2(stext, bp - stext));
  621. if(stext != st0) free(stext);
  622. return TEXT;
  623. }
  624. static int mkComment(int c)
  625. {
  626. char st0[INITBUFSIZE];
  627. unsigned int nstext = INITBUFSIZE;
  628. char *stext = st0, *bp = st0;
  629. do TEXT_PUSH(c);
  630. while ((c = xxgetc()) != '\n' && c != R_EOF);
  631. if (c == R_EOF) xxungetc(c);
  632. else TEXT_PUSH(c);
  633. PROTECT(yylval = mkString2(stext, bp - stext));
  634. if(stext != st0) free(stext);
  635. return COMMENT;
  636. }
  637. static int mkMarkup(int c)
  638. {
  639. char st0[INITBUFSIZE];
  640. unsigned int nstext = INITBUFSIZE;
  641. char *stext = st0, *bp = st0;
  642. int retval = 0;
  643. TEXT_PUSH(c);
  644. while (isalpha((c = xxgetc()))) TEXT_PUSH(c);
  645. /* One non-alpha allowed */
  646. if (bp - stext == 1) {
  647. TEXT_PUSH(c);
  648. TEXT_PUSH('\0');
  649. retval = MACRO;
  650. } else {
  651. TEXT_PUSH('\0');
  652. retval = KeywordLookup(stext);
  653. if (retval == VERB)
  654. retval = mkVerb(c); /* This makes the yylval */
  655. else if (c != ' ') /* Eat a space, but keep other terminators */
  656. xxungetc(c);
  657. }
  658. if (retval != VERB) {
  659. PROTECT(yylval = mkString(stext));
  660. }
  661. if(stext != st0) free(stext);
  662. return retval;
  663. }
  664. static int mkVerb(int c)
  665. {
  666. char st0[INITBUFSIZE];
  667. unsigned int nstext = INITBUFSIZE;
  668. char *stext = st0, *bp = st0;
  669. int delim = c;
  670. TEXT_PUSH('\\'); TEXT_PUSH('v'); TEXT_PUSH('e'); TEXT_PUSH('r'); TEXT_PUSH('b');
  671. TEXT_PUSH(c);
  672. while ((c = xxgetc()) != delim) TEXT_PUSH(c);
  673. TEXT_PUSH(c);
  674. PROTECT(yylval = mkString2(stext, bp - stext));
  675. if(stext != st0) free(stext);
  676. return VERB;
  677. }
  678. static int mkVerbEnv()
  679. {
  680. char st0[INITBUFSIZE];
  681. unsigned int nstext = INITBUFSIZE;
  682. char *stext = st0, *bp = st0;
  683. int matched = 0, i;
  684. int c;
  685. while ((c = xxgetc()) != R_EOF && CHAR(STRING_ELT(xxInVerbEnv, 0))[matched]) {
  686. TEXT_PUSH(c);
  687. if (c == CHAR(STRING_ELT(xxInVerbEnv, 0))[matched])
  688. matched++;
  689. else
  690. matched = 0;
  691. }
  692. if ( !CHAR(STRING_ELT(xxInVerbEnv, 0))[matched] ) {
  693. for (i = matched-1; i >= 0; i--)
  694. xxungetc(*(--bp));
  695. UNPROTECT_PTR(xxInVerbEnv);
  696. xxInVerbEnv = NULL;
  697. }
  698. PROTECT(yylval = mkString2(stext, bp - stext));
  699. if (stext != st0) free(stext);
  700. return VERB;
  701. }
  702. static int yylex(void)
  703. {
  704. int tok = token();
  705. if (xxDebugTokens) {
  706. Rprintf("%d:%d: %s", yylloc.first_line, yylloc.first_column, yytname[YYTRANSLATE(tok)]);
  707. if (tok > 255 && tok != END_OF_INPUT)
  708. Rprintf(": %s", CHAR(STRING_ELT(yylval, 0)));
  709. Rprintf("\n");
  710. }
  711. setlastloc();
  712. return tok;
  713. }
  714. /* "do_parseRd"
  715. .Internal( parseLatex(file, srcfile, verbose, basename, warningCalls) )
  716. If there is text then that is read and the other arguments are ignored.
  717. */
  718. SEXP attribute_hidden do_parseLatex(SEXP call, SEXP op, SEXP args, SEXP env)
  719. {
  720. SEXP s = R_NilValue, source, text;
  721. ParseStatus status;
  722. #if DEBUGMODE
  723. yydebug = 1;
  724. #endif
  725. checkArity(op, args);
  726. R_ParseError = 0;
  727. R_ParseErrorMsg[0] = '\0';
  728. text = CAR(args); args = CDR(args);
  729. source = CAR(args); args = CDR(args);
  730. if(!isLogical(CAR(args)) || LENGTH(CAR(args)) != 1)
  731. error(_("invalid '%s' value"), "verbose");
  732. xxDebugTokens = asInteger(CAR(args)); args = CDR(args);
  733. xxVerbatimList = CAR(args); args = CDR(args);
  734. s = R_ParseLatex(text, &status, source);
  735. if (status != PARSE_OK) parseError(call, R_ParseError);
  736. return s;
  737. }