PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Happy | 1862 lines | 1598 code | 264 blank | 0 comment | 0 complexity | e27bf6725fb216542bbb90b032017cb0 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

Large files files are truncated, but you can click here to view the full file

  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--2011 The R Core Team
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, a copy is available at
  19. * http://www.r-project.org/Licenses/
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #define R_USE_SIGNALS 1
  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. #include <Rmath.h> /* for imax2(.),..*/
  31. /* bison creates a non-static symbol yylloc in both gramLatex.o and gramRd.o,
  32. so remap */
  33. #define yylloc yyllocR
  34. #define DEBUGVALS 0 /* 1 causes detailed internal state output to R console */
  35. #define DEBUGMODE 0 /* 1 causes Bison output of parse state, to stdout or stderr */
  36. static Rboolean wCalls = TRUE;
  37. #define YYERROR_VERBOSE 1
  38. static void yyerror(const char *);
  39. static int yylex();
  40. static int yyparse(void);
  41. #define yyconst const
  42. typedef struct yyltype
  43. {
  44. int first_line;
  45. int first_column;
  46. int first_byte;
  47. int last_line;
  48. int last_column;
  49. int last_byte;
  50. } yyltype;
  51. # define YYLTYPE yyltype
  52. # define YYLLOC_DEFAULT(Current, Rhs, N) \
  53. do \
  54. if (YYID (N)) \
  55. { \
  56. (Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
  57. (Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
  58. (Current).first_byte = YYRHSLOC (Rhs, 1).first_byte; \
  59. (Current).last_line = YYRHSLOC (Rhs, N).last_line; \
  60. (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
  61. (Current).last_byte = YYRHSLOC (Rhs, N).last_byte; \
  62. } \
  63. else \
  64. { \
  65. (Current).first_line = (Current).last_line = \
  66. YYRHSLOC (Rhs, 0).last_line; \
  67. (Current).first_column = (Current).last_column = \
  68. YYRHSLOC (Rhs, 0).last_column; \
  69. (Current).first_byte = (Current).last_byte = \
  70. YYRHSLOC (Rhs, 0).last_byte; \
  71. } \
  72. while (YYID (0))
  73. /* Useful defines so editors don't get confused ... */
  74. #define LBRACE '{'
  75. #define RBRACE '}'
  76. /* Functions used in the parsing process */
  77. static SEXP GrowList(SEXP, SEXP);
  78. static int KeywordLookup(const char *);
  79. static SEXP UserMacroLookup(const char *);
  80. static SEXP InstallKeywords();
  81. static SEXP NewList(void);
  82. static SEXP makeSrcref(YYLTYPE *, SEXP);
  83. /* Flags used to mark need for postprocessing in the dynamicFlag attribute */
  84. #define STATIC 0
  85. #define HAS_IFDEF 1
  86. #define HAS_SEXPR 2
  87. /* Internal lexer / parser state variables */
  88. static int xxinRString, xxQuoteLine, xxQuoteCol;
  89. static int xxinEqn;
  90. static int xxNewlineInString;
  91. static int xxgetc();
  92. static int xxungetc(int);
  93. static int xxlineno, xxbyteno, xxcolno;
  94. static int xxmode, xxitemType, xxbraceDepth; /* context for lexer */
  95. static int xxDebugTokens; /* non-zero causes debug output to R console */
  96. static const char* xxBasename; /* basename of file for error messages */
  97. static SEXP Value;
  98. static int xxinitvalue;
  99. static char const yyunknown[] = "unknown macro"; /* our message, not bison's */
  100. static SEXP xxMacroList;/* A hashed environment containing all the standard and user-defined macro names */
  101. #define RLIKE 1 /* Includes R strings; xxinRString holds the opening quote char, or 0 outside a string */
  102. #define LATEXLIKE 2
  103. #define VERBATIM 3
  104. #define INOPTION 4
  105. #define COMMENTMODE 5 /* only used in deparsing */
  106. #define UNKNOWNMODE 6 /* ditto */
  107. static SEXP SrcFile; /* parse_Rd will *always* supply a srcfile */
  108. /* Routines used to build the parse tree */
  109. static SEXP xxpushMode(int, int, int);
  110. static void xxpopMode(SEXP);
  111. static SEXP xxnewlist(SEXP);
  112. static SEXP xxnewlist2(SEXP, SEXP);
  113. static SEXP xxnewlist3(SEXP, SEXP, SEXP);
  114. static SEXP xxnewlist4(SEXP, SEXP, SEXP, SEXP);
  115. static SEXP xxnewlist5(SEXP, SEXP, SEXP, SEXP, SEXP);
  116. static SEXP xxnewlist6(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
  117. static SEXP xxnewlist7(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
  118. static SEXP xxnewlist8(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
  119. static SEXP xxnewlist9(SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP, SEXP);
  120. static SEXP xxlist(SEXP, SEXP);
  121. static SEXP xxmarkup(SEXP, SEXP, int, YYLTYPE *);
  122. static SEXP xxmarkup2(SEXP, SEXP, SEXP, int, int, YYLTYPE *);
  123. static SEXP xxmarkup3(SEXP, SEXP, SEXP, SEXP, int, YYLTYPE *);
  124. static SEXP xxOptionmarkup(SEXP, SEXP, SEXP, int, YYLTYPE *);
  125. static SEXP xxtag(SEXP, int, YYLTYPE *);
  126. static void xxsavevalue(SEXP, YYLTYPE *);
  127. static void xxWarnNewline();
  128. static SEXP xxnewcommand(SEXP, SEXP, SEXP, YYLTYPE *);
  129. static SEXP xxusermacro(SEXP, SEXP, YYLTYPE *);
  130. static int mkMarkup(int);
  131. static int mkIfdef(int);
  132. static int mkCode(int);
  133. static int mkText(int);
  134. static int mkVerb(int);
  135. static int mkComment(int);
  136. #define YYSTYPE SEXP
  137. %}
  138. %debug
  139. %token END_OF_INPUT ERROR
  140. %token SECTIONHEADER RSECTIONHEADER VSECTIONHEADER
  141. %token SECTIONHEADER2
  142. %token RCODEMACRO SEXPR RDOPTS LATEXMACRO VERBMACRO OPTMACRO ESCAPE
  143. %token LISTSECTION ITEMIZE DESCRIPTION NOITEM
  144. %token LATEXMACRO2 VERBMACRO2 VERBLATEX
  145. %token LATEXMACRO3
  146. %token NEWCOMMAND USERMACRO USERMACRO1 USERMACRO2 USERMACRO3 USERMACRO4
  147. %token USERMACRO5 USERMACRO6 USERMACRO7 USERMACRO8 USERMACRO9
  148. %token IFDEF ENDIF
  149. %token TEXT RCODE VERB COMMENT UNKNOWN
  150. %token STARTFILE STARTFRAGMENT /* fake tokens to have two entry points */
  151. /* Recent bison has <> to represent all of the destructors below, but we don't assume it */
  152. /* I think we need to list everything here which occurs before the last item in a
  153. pattern, just in case the last item is unmatched and we need to back out. But
  154. it is safe to list more, so we do. */
  155. %destructor { UNPROTECT_PTR($$); } SECTIONHEADER RSECTIONHEADER
  156. VSECTIONHEADER SECTIONHEADER2 RCODEMACRO SEXPR LATEXMACRO VERBMACRO
  157. OPTMACRO ESCAPE LISTSECTION ITEMIZE DESCRIPTION NOITEM LATEXMACRO2
  158. VERBMACRO2 VERBLATEX LATEXMACRO3 IFDEF ENDIF TEXT RCODE VERB COMMENT UNKNOWN
  159. NEWCOMMAND USERMACRO USERMACRO1 USERMACRO2 USERMACRO3 USERMACRO4
  160. USERMACRO5 USERMACRO6 USERMACRO7 USERMACRO8 USERMACRO9
  161. STARTFILE STARTFRAGMENT goLatexLike goRLike goRLike2 goOption
  162. goVerbatim goVerbatim1 goVerbatim2 goItem0 goItem2 LatexArg RLikeArg2
  163. VerbatimArg1 VerbatimArg2 IfDefTarget ArgItems Option
  164. %%
  165. Init: STARTFILE RdFile END_OF_INPUT { xxsavevalue($2, &@$); UNPROTECT_PTR($1); return 0; }
  166. | STARTFRAGMENT RdFragment END_OF_INPUT { xxsavevalue($2, &@$); UNPROTECT_PTR($1); return 0; }
  167. | error { PROTECT(Value = R_NilValue); YYABORT; }
  168. ;
  169. RdFragment : goLatexLike ArgItems { $$ = $2; UNPROTECT_PTR($1); }
  170. ;
  171. RdFile : SectionList { $$ = $1; }
  172. ;
  173. SectionList: Section { $$ = xxnewlist($1); }
  174. | SectionList Section { $$ = xxlist($1, $2); }
  175. Section: VSECTIONHEADER VerbatimArg { $$ = xxmarkup($1, $2, STATIC, &@$); }
  176. | RDOPTS VerbatimArg { $$ = xxmarkup($1, $2, HAS_SEXPR, &@$); }
  177. | RSECTIONHEADER RLikeArg { $$ = xxmarkup($1, $2, STATIC, &@$); }
  178. | SECTIONHEADER LatexArg { $$ = xxmarkup($1, $2, STATIC, &@$); }
  179. | LISTSECTION Item2Arg { $$ = xxmarkup($1, $2, STATIC, &@$); }
  180. | SECTIONHEADER2 LatexArg LatexArg2 { $$ = xxmarkup2($1, $2, $3, 2, STATIC, &@$); }
  181. | IFDEF IfDefTarget SectionList ENDIF { $$ = xxmarkup2($1, $2, $3, 2, HAS_IFDEF, &@$); UNPROTECT_PTR($4); }
  182. | IFDEF IfDefTarget SectionList error { $$ = xxmarkup2($1, $2, $3, 2, HAS_IFDEF, &@$); }
  183. | SEXPR goOption RLikeArg2 { $$ = xxmarkup($1, $3, HAS_SEXPR, &@$); xxpopMode($2); }
  184. | SEXPR goOption Option RLikeArg2 { $$ = xxOptionmarkup($1, $3, $4, HAS_SEXPR, &@$); xxpopMode($2); }
  185. | COMMENT { $$ = xxtag($1, COMMENT, &@$); }
  186. | TEXT { $$ = xxtag($1, TEXT, &@$); } /* must be whitespace */
  187. | UserMacro { $$ = $1; }
  188. | error Section { $$ = $2; }
  189. ArgItems: Item { $$ = xxnewlist($1); }
  190. | ArgItems Item { $$ = xxlist($1, $2); }
  191. Item: TEXT { $$ = xxtag($1, TEXT, &@$); }
  192. | RCODE { $$ = xxtag($1, RCODE, &@$); }
  193. | VERB { $$ = xxtag($1, VERB, &@$); }
  194. | COMMENT { $$ = xxtag($1, COMMENT, &@$); }
  195. | UNKNOWN { $$ = xxtag($1, UNKNOWN, &@$); yyerror(yyunknown); }
  196. | Arg { $$ = xxmarkup(R_NilValue, $1, STATIC, &@$); }
  197. | Markup { $$ = $1; }
  198. | UserMacro { $$ = $1; }
  199. | error Item { $$ = $2; }
  200. Markup: LATEXMACRO LatexArg { $$ = xxmarkup($1, $2, STATIC, &@$); }
  201. | LATEXMACRO2 LatexArg LatexArg2 { $$ = xxmarkup2($1, $2, $3, 2, STATIC, &@$); }
  202. | LATEXMACRO3 LatexArg LatexArg2 LatexArg2 { $$ = xxmarkup3($1, $2, $3, $4, STATIC, &@$); }
  203. | ITEMIZE Item0Arg { $$ = xxmarkup($1, $2, STATIC, &@$); }
  204. | DESCRIPTION Item2Arg { $$ = xxmarkup($1, $2, STATIC, &@$); }
  205. | OPTMACRO goOption LatexArg { $$ = xxmarkup($1, $3, STATIC, &@$); xxpopMode($2); }
  206. | OPTMACRO goOption Option LatexArg { $$ = xxOptionmarkup($1, $3, $4, STATIC, &@$); xxpopMode($2); }
  207. | RCODEMACRO RLikeArg { $$ = xxmarkup($1, $2, STATIC, &@$); }
  208. | SEXPR goOption RLikeArg2 { $$ = xxmarkup($1, $3, HAS_SEXPR, &@$); xxpopMode($2); }
  209. | SEXPR goOption Option RLikeArg2 { $$ = xxOptionmarkup($1, $3, $4, HAS_SEXPR, &@$); xxpopMode($2); }
  210. | VERBMACRO VerbatimArg { $$ = xxmarkup($1, $2, STATIC, &@$); }
  211. | VERBMACRO2 VerbatimArg1 { $$ = xxmarkup2($1, $2, R_NilValue, 1, STATIC, &@$); }
  212. | VERBMACRO2 VerbatimArg1 VerbatimArg2 { $$ = xxmarkup2($1, $2, $3, 2, STATIC, &@$); }
  213. | ESCAPE { $$ = xxmarkup($1, R_NilValue, STATIC, &@$); }
  214. | IFDEF IfDefTarget ArgItems ENDIF { $$ = xxmarkup2($1, $2, $3, 2, HAS_IFDEF, &@$); UNPROTECT_PTR($4); }
  215. | IFDEF IfDefTarget ArgItems error { $$ = xxmarkup2($1, $2, $3, 2, HAS_IFDEF, &@$); }
  216. | VERBLATEX VerbatimArg1 LatexArg2 { $$ = xxmarkup2($1, $2, $3, 2, STATIC, &@$); }
  217. UserMacro: NEWCOMMAND VerbatimArg1 VerbatimArg { $$ = xxnewcommand($1, $2, $3, &@$); }
  218. | USERMACRO { $$ = xxusermacro($1, xxnewlist(NULL), &@$); }
  219. | USERMACRO1 VerbatimArg { $$ = xxusermacro($1, xxnewlist($2), &@$); }
  220. | USERMACRO2 VerbatimArg VerbatimArg
  221. { $$ = xxusermacro($1, xxnewlist2($2, $3), &@$); }
  222. | USERMACRO3 VerbatimArg VerbatimArg VerbatimArg
  223. { $$ = xxusermacro($1, xxnewlist3($2, $3, $4), &@$); }
  224. | USERMACRO4 VerbatimArg VerbatimArg VerbatimArg VerbatimArg
  225. { $$ = xxusermacro($1, xxnewlist4($2, $3, $4, $5), &@$); }
  226. | USERMACRO5 VerbatimArg VerbatimArg VerbatimArg VerbatimArg VerbatimArg
  227. { $$ = xxusermacro($1, xxnewlist5($2, $3, $4, $5, $6), &@$); }
  228. | USERMACRO6 VerbatimArg VerbatimArg VerbatimArg VerbatimArg VerbatimArg
  229. VerbatimArg { $$ = xxusermacro($1, xxnewlist6($2, $3, $4, $5, $6, $7), &@$); }
  230. | USERMACRO7 VerbatimArg VerbatimArg VerbatimArg VerbatimArg VerbatimArg VerbatimArg
  231. VerbatimArg VerbatimArg
  232. { $$ = xxusermacro($1, xxnewlist7($2, $3, $4, $5, $6, $7, $8), &@$); }
  233. | USERMACRO8 VerbatimArg VerbatimArg VerbatimArg VerbatimArg VerbatimArg VerbatimArg
  234. VerbatimArg VerbatimArg VerbatimArg
  235. { $$ = xxusermacro($1, xxnewlist8($2, $3, $4, $5, $6, $7, $8, $9), &@$); }
  236. | USERMACRO9 VerbatimArg VerbatimArg VerbatimArg VerbatimArg VerbatimArg VerbatimArg
  237. VerbatimArg VerbatimArg VerbatimArg VerbatimArg
  238. { $$ = xxusermacro($1, xxnewlist9($2, $3, $4, $5, $6, $7, $8, $9, $10), &@$); }
  239. LatexArg: goLatexLike Arg { xxpopMode($1); $$ = $2; }
  240. LatexArg2: goLatexLike Arg { xxpopMode($1); $$ = $2; }
  241. | goLatexLike TEXT { xxpopMode($1); $$ = xxnewlist($2);
  242. if(wCalls)
  243. warning(_("bad markup (extra space?) at %s:%d:%d"),
  244. xxBasename, @2.first_line, @2.first_column);
  245. else
  246. warningcall(R_NilValue, _("bad markup (extra space?) at %s:%d:%d"),
  247. xxBasename, @2.first_line, @2.first_column);
  248. }
  249. Item0Arg: goItem0 Arg { xxpopMode($1); $$ = $2; }
  250. Item2Arg: goItem2 Arg { xxpopMode($1); $$ = $2; }
  251. RLikeArg: goRLike Arg { xxpopMode($1); $$ = $2; }
  252. /* This one is like VerbatimArg2 below: it does the push after seeing the brace */
  253. RLikeArg2: '{' goRLike2 ArgItems '}' { xxpopMode($2); $$ = $3; }
  254. | '{' goRLike2 '}' { xxpopMode($2); $$ = xxnewlist(NULL); }
  255. VerbatimArg: goVerbatim Arg { xxpopMode($1); $$ = $2; }
  256. VerbatimArg1: goVerbatim1 Arg { xxpopMode($1); $$ = $2; }
  257. /* This one executes the push after seeing the brace starting the optional second arg */
  258. VerbatimArg2: '{' goVerbatim2 ArgItems '}' { xxpopMode($2); $$ = $3; }
  259. | '{' goVerbatim2 '}' { xxpopMode($2); $$ = xxnewlist(NULL); }
  260. IfDefTarget: goLatexLike TEXT { xxpopMode($1); $$ = xxnewlist(xxtag($2, TEXT, &@$)); }
  261. goLatexLike: /* empty */ { $$ = xxpushMode(LATEXLIKE, UNKNOWN, FALSE); }
  262. goRLike: /* empty */ { $$ = xxpushMode(RLIKE, UNKNOWN, FALSE); }
  263. goRLike2: /* empty */ { xxbraceDepth--; $$ = xxpushMode(RLIKE, UNKNOWN, FALSE); xxbraceDepth++; }
  264. goOption: /* empty */ { $$ = xxpushMode(INOPTION, UNKNOWN, FALSE); }
  265. goVerbatim: /* empty */ { $$ = xxpushMode(VERBATIM, UNKNOWN, FALSE); }
  266. goVerbatim1: /* empty */ { $$ = xxpushMode(VERBATIM, UNKNOWN, TRUE); }
  267. goVerbatim2: /* empty */ { xxbraceDepth--; $$ = xxpushMode(VERBATIM, UNKNOWN, FALSE); xxbraceDepth++; }
  268. goItem0: /* empty */ { $$ = xxpushMode(LATEXLIKE, ESCAPE, FALSE); }
  269. goItem2: /* empty */ { $$ = xxpushMode(LATEXLIKE, LATEXMACRO2, FALSE); }
  270. Arg: '{' ArgItems '}' { $$ = $2; }
  271. | '{' '}' { $$ = xxnewlist(NULL); }
  272. | '{' ArgItems error '}' { $$ = $2; }
  273. | '{' error '}' { $$ = xxnewlist(NULL); }
  274. | '{' ArgItems error END_OF_INPUT { $$ = $2; }
  275. Option: '[' Item ']' { $$ = $2; }
  276. %%
  277. static SEXP xxpushMode(int newmode, int newitem, int neweqn)
  278. {
  279. SEXP ans;
  280. PROTECT(ans = allocVector(INTSXP, 7));
  281. INTEGER(ans)[0] = xxmode; /* Lexer mode */
  282. INTEGER(ans)[1] = xxitemType; /* What is \item? */
  283. INTEGER(ans)[2] = xxbraceDepth; /* Brace depth used in RCODE and VERBATIM */
  284. INTEGER(ans)[3] = xxinRString; /* Quote char that started a string */
  285. INTEGER(ans)[4] = xxQuoteLine; /* Where the quote was */
  286. INTEGER(ans)[5] = xxQuoteCol; /* " */
  287. INTEGER(ans)[6] = xxinEqn; /* In the first arg to \eqn or \deqn: no escapes */
  288. #if DEBUGMODE
  289. Rprintf("xxpushMode(%d, %s) pushes %d, %s, %d\n", newmode, yytname[YYTRANSLATE(newitem)],
  290. xxmode, yytname[YYTRANSLATE(xxitemType)], xxbraceDepth);
  291. #endif
  292. xxmode = newmode;
  293. xxitemType = newitem;
  294. xxbraceDepth = 0;
  295. xxinRString = 0;
  296. xxinEqn = neweqn;
  297. return ans;
  298. }
  299. static void xxpopMode(SEXP oldmode)
  300. {
  301. #if DEBUGVALS
  302. Rprintf("xxpopMode(%d, %s, %d) replaces %d, %s, %d\n", INTEGER(oldmode)[0], yytname[YYTRANSLATE(INTEGER(oldmode)[1])], INTEGER(oldmode)[2],
  303. xxmode, yytname[YYTRANSLATE(xxitemType)], xxbraceDepth);
  304. #endif
  305. xxmode = INTEGER(oldmode)[0];
  306. xxitemType = INTEGER(oldmode)[1];
  307. xxbraceDepth = INTEGER(oldmode)[2];
  308. xxinRString = INTEGER(oldmode)[3];
  309. xxQuoteLine = INTEGER(oldmode)[4];
  310. xxQuoteCol = INTEGER(oldmode)[5];
  311. xxinEqn = INTEGER(oldmode)[6];
  312. UNPROTECT_PTR(oldmode);
  313. }
  314. static int getDynamicFlag(SEXP item)
  315. {
  316. SEXP flag = getAttrib(item, install("dynamicFlag"));
  317. if (isNull(flag)) return 0;
  318. else return INTEGER(flag)[0];
  319. }
  320. static void setDynamicFlag(SEXP item, int flag)
  321. {
  322. if (flag)
  323. setAttrib(item, install("dynamicFlag"), ScalarInteger(flag));
  324. }
  325. static SEXP xxnewlist(SEXP item)
  326. {
  327. SEXP ans, tmp;
  328. #if DEBUGVALS
  329. Rprintf("xxnewlist(item=%p)", item);
  330. #endif
  331. PROTECT(tmp = NewList());
  332. if (item) {
  333. int flag = getDynamicFlag(item);
  334. PROTECT(ans = GrowList(tmp, item));
  335. setDynamicFlag(ans, flag);
  336. UNPROTECT_PTR(tmp);
  337. UNPROTECT_PTR(item);
  338. } else ans = tmp;
  339. #if DEBUGVALS
  340. Rprintf(" result: %p is length %d\n", ans, length(ans));
  341. #endif
  342. return ans;
  343. }
  344. static SEXP xxnewlist2(SEXP item1, SEXP item2)
  345. {
  346. return xxlist(xxnewlist(item1), item2);
  347. }
  348. static SEXP xxnewlist3(SEXP item1, SEXP item2, SEXP item3)
  349. {
  350. return xxlist(xxnewlist2(item1, item2), item3);
  351. }
  352. static SEXP xxnewlist4(SEXP item1, SEXP item2, SEXP item3, SEXP item4)
  353. {
  354. return xxlist(xxnewlist3(item1, item2, item3), item4);
  355. }
  356. static SEXP xxnewlist5(SEXP item1, SEXP item2, SEXP item3, SEXP item4, SEXP item5)
  357. {
  358. return xxlist(xxnewlist4(item1, item2, item3, item4), item5);
  359. }
  360. static SEXP xxnewlist6(SEXP item1, SEXP item2, SEXP item3, SEXP item4, SEXP item5,
  361. SEXP item6)
  362. {
  363. return xxlist(xxnewlist5(item1, item2, item3, item4, item5), item6);
  364. }
  365. static SEXP xxnewlist7(SEXP item1, SEXP item2, SEXP item3, SEXP item4, SEXP item5,
  366. SEXP item6, SEXP item7)
  367. {
  368. return xxlist(xxnewlist6(item1, item2, item3, item4, item5, item6), item7);
  369. }
  370. static SEXP xxnewlist8(SEXP item1, SEXP item2, SEXP item3, SEXP item4, SEXP item5,
  371. SEXP item6, SEXP item7, SEXP item8)
  372. {
  373. return xxlist(xxnewlist7(item1, item2, item3, item4, item5, item6, item7), item8);
  374. }
  375. static SEXP xxnewlist9(SEXP item1, SEXP item2, SEXP item3, SEXP item4, SEXP item5,
  376. SEXP item6, SEXP item7, SEXP item8, SEXP item9)
  377. {
  378. return xxlist(xxnewlist8(item1, item2, item3, item4, item5, item6, item7, item8),
  379. item9);
  380. }
  381. static SEXP xxlist(SEXP oldlist, SEXP item)
  382. {
  383. SEXP ans;
  384. int flag = getDynamicFlag(oldlist) | getDynamicFlag(item);
  385. #if DEBUGVALS
  386. Rprintf("xxlist(oldlist=%p, item=%p)", oldlist, item);
  387. #endif
  388. PROTECT(ans = GrowList(oldlist, item));
  389. UNPROTECT_PTR(item);
  390. UNPROTECT_PTR(oldlist);
  391. setDynamicFlag(ans, flag);
  392. #if DEBUGVALS
  393. Rprintf(" result: %p is length %d\n", ans, length(ans));
  394. #endif
  395. return ans;
  396. }
  397. static SEXP xxmarkup(SEXP header, SEXP body, int flag, YYLTYPE *lloc)
  398. {
  399. SEXP ans;
  400. #if DEBUGVALS
  401. Rprintf("xxmarkup(header=%p, body=%p)", header, body);
  402. #endif
  403. if (isNull(body))
  404. PROTECT(ans = allocVector(VECSXP, 0));
  405. else {
  406. flag |= getDynamicFlag(body);
  407. PROTECT(ans = PairToVectorList(CDR(body)));
  408. UNPROTECT_PTR(body);
  409. }
  410. if (isNull(header))
  411. PROTECT(header = mkString("LIST"));
  412. setAttrib(ans, install("Rd_tag"), header);
  413. setAttrib(ans, R_SrcrefSymbol, makeSrcref(lloc, SrcFile));
  414. UNPROTECT_PTR(header);
  415. setDynamicFlag(ans, flag);
  416. #if DEBUGVALS
  417. Rprintf(" result: %p\n", ans);
  418. #endif
  419. return ans;
  420. }
  421. static SEXP xxnewcommand(SEXP cmd, SEXP name, SEXP defn, YYLTYPE *lloc)
  422. {
  423. SEXP ans, prev, thename, thedefn;
  424. char buffer[128];
  425. const char *c;
  426. int maxarg = 0;
  427. #if DEBUGVALS
  428. Rprintf("xxnewcommand(cmd=%p, name=%p, defn=%p)", cmd, name, defn);
  429. #endif
  430. thename = CADR(name);
  431. thedefn = CADR(defn);
  432. if (TYPEOF(thedefn) == STRSXP)
  433. PROTECT(thedefn = mkString(CHAR(STRING_ELT(thedefn,0))));
  434. else
  435. PROTECT(thedefn = mkString(""));
  436. prev = findVar(install(CHAR(STRING_ELT(thename, 0))), xxMacroList);
  437. if (prev != R_UnboundValue && !strcmp(CHAR(STRING_ELT(cmd,0)), "\renewcommand")) {
  438. snprintf(buffer, sizeof(buffer), _("Macro '%s' previously defined."),
  439. CHAR(STRING_ELT(thename, 0)));
  440. yyerror(buffer);
  441. }
  442. for (c = CHAR(STRING_ELT(thedefn, 0)); *c; c++) {
  443. if (*c == '#' && isdigit(*(c+1)))
  444. maxarg = imax2(maxarg, *(c+1) - '0');
  445. }
  446. if (maxarg > 4) {
  447. snprintf(buffer, sizeof(buffer), _("At most 4 arguments are allowed for user defined macros."));
  448. yyerror(buffer);
  449. }
  450. PROTECT(ans = ScalarInteger(USERMACRO + maxarg));
  451. setAttrib(ans, install("Rd_tag"), cmd);
  452. setAttrib(ans, install("definition"), thedefn);
  453. setAttrib(ans, R_SrcrefSymbol, makeSrcref(lloc, SrcFile));
  454. defineVar(install(CHAR(STRING_ELT(thename, 0))), ans, xxMacroList);
  455. UNPROTECT_PTR(thedefn);
  456. UNPROTECT_PTR(cmd);
  457. UNPROTECT_PTR(name);
  458. UNPROTECT_PTR(defn);
  459. return ans;
  460. }
  461. #define START_MACRO -2
  462. #define END_MACRO -3
  463. static SEXP xxusermacro(SEXP macro, SEXP args, YYLTYPE *lloc)
  464. {
  465. SEXP ans, value, nextarg;
  466. int i,len;
  467. const char *c, *start ;
  468. #if DEBUGVALS
  469. Rprintf("xxusermacro(macro=%p, args=%p)", macro, args);
  470. #endif
  471. len = length(args)-1;
  472. PROTECT(ans = allocVector(STRSXP, len + 1));
  473. value = UserMacroLookup(CHAR(STRING_ELT(macro,0)));
  474. if (TYPEOF(value) == STRSXP)
  475. SET_STRING_ELT(ans, 0, STRING_ELT(value, 0));
  476. else
  477. error(_("No macro definition for '%s'."), CHAR(STRING_ELT(macro,0)));
  478. /* Rprintf("len = %d", len); */
  479. for (i = 0, nextarg=args; i < len; i++, nextarg = CDR(nextarg)) {
  480. /* Rprintf("arg i is");
  481. PrintValue(CADR(CADR(nextarg))); */
  482. SET_STRING_ELT(ans, i+1, STRING_ELT(CADR(CADR(nextarg)), 0));
  483. }
  484. UNPROTECT_PTR(args);
  485. UNPROTECT_PTR(macro);
  486. /* Now push the expanded macro onto the input stream, in reverse order */
  487. xxungetc(END_MACRO);
  488. start = CHAR(STRING_ELT(ans, 0));
  489. for (c = start + strlen(start); c > start; c--) {
  490. if (c > start + 1 && *(c-2) == '#' && isdigit(*(c-1))) {
  491. int which = *(c-1) - '0';
  492. const char *arg = CHAR(STRING_ELT(ans, which));
  493. for (i = strlen(arg); i > 0; i--) {
  494. xxungetc(arg[i-1]);
  495. }
  496. c--;
  497. } else {
  498. xxungetc(*(c-1));
  499. }
  500. }
  501. xxungetc(START_MACRO);
  502. setAttrib(ans, install("Rd_tag"), mkString("USERMACRO"));
  503. setAttrib(ans, R_SrcrefSymbol, makeSrcref(lloc, SrcFile));
  504. #if DEBUGVALS
  505. Rprintf(" result: %p\n", ans);
  506. #endif
  507. return ans;
  508. }
  509. static SEXP xxOptionmarkup(SEXP header, SEXP option, SEXP body, int flag, YYLTYPE *lloc)
  510. {
  511. SEXP ans;
  512. #if DEBUGVALS
  513. Rprintf("xxOptionmarkup(header=%p, option=%p, body=%p)", header, option, body);
  514. #endif
  515. flag |= getDynamicFlag(body);
  516. PROTECT(ans = PairToVectorList(CDR(body)));
  517. UNPROTECT_PTR(body);
  518. setAttrib(ans, install("Rd_tag"), header);
  519. UNPROTECT_PTR(header);
  520. flag |= getDynamicFlag(option);
  521. setAttrib(ans, install("Rd_option"), option);
  522. UNPROTECT_PTR(option);
  523. setAttrib(ans, R_SrcrefSymbol, makeSrcref(lloc, SrcFile));
  524. setDynamicFlag(ans, flag);
  525. #if DEBUGVALS
  526. Rprintf(" result: %p\n", ans);
  527. #endif
  528. return ans;
  529. }
  530. static SEXP xxmarkup2(SEXP header, SEXP body1, SEXP body2, int argcount, int flag, YYLTYPE *lloc)
  531. {
  532. SEXP ans;
  533. #if DEBUGVALS
  534. Rprintf("xxmarkup2(header=%p, body1=%p, body2=%p)", header, body1, body2);
  535. #endif
  536. PROTECT(ans = allocVector(VECSXP, argcount));
  537. if (!isNull(body1)) {
  538. int flag1 = getDynamicFlag(body1);
  539. SET_VECTOR_ELT(ans, 0, PairToVectorList(CDR(body1)));
  540. UNPROTECT_PTR(body1);
  541. setDynamicFlag(VECTOR_ELT(ans, 0), flag1);
  542. flag |= flag1;
  543. }
  544. if (!isNull(body2)) {
  545. int flag2;
  546. if (argcount < 2) error("internal error: inconsistent argument count");
  547. flag2 = getDynamicFlag(body2);
  548. SET_VECTOR_ELT(ans, 1, PairToVectorList(CDR(body2)));
  549. UNPROTECT_PTR(body2);
  550. setDynamicFlag(VECTOR_ELT(ans, 1), flag2);
  551. flag |= flag2;
  552. }
  553. setAttrib(ans, install("Rd_tag"), header);
  554. UNPROTECT_PTR(header);
  555. setAttrib(ans, R_SrcrefSymbol, makeSrcref(lloc, SrcFile));
  556. setDynamicFlag(ans, flag);
  557. #if DEBUGVALS
  558. Rprintf(" result: %p\n", ans);
  559. #endif
  560. return ans;
  561. }
  562. static SEXP xxmarkup3(SEXP header, SEXP body1, SEXP body2, SEXP body3, int flag, YYLTYPE *lloc)
  563. {
  564. SEXP ans;
  565. #if DEBUGVALS
  566. Rprintf("xxmarkup2(header=%p, body1=%p, body2=%p, body3=%p)", header, body1, body2, body3);
  567. #endif
  568. PROTECT(ans = allocVector(VECSXP, 3));
  569. if (!isNull(body1)) {
  570. int flag1 = getDynamicFlag(body1);
  571. SET_VECTOR_ELT(ans, 0, PairToVectorList(CDR(body1)));
  572. UNPROTECT_PTR(body1);
  573. setDynamicFlag(VECTOR_ELT(ans, 0), flag1);
  574. flag |= flag1;
  575. }
  576. if (!isNull(body2)) {
  577. int flag2;
  578. flag2 = getDynamicFlag(body2);
  579. SET_VECTOR_ELT(ans, 1, PairToVectorList(CDR(body2)));
  580. UNPROTECT_PTR(body2);
  581. setDynamicFlag(VECTOR_ELT(ans, 1), flag2);
  582. flag |= flag2;
  583. }
  584. if (!isNull(body3)) {
  585. int flag3;
  586. flag3 = getDynamicFlag(body3);
  587. SET_VECTOR_ELT(ans, 2, PairToVectorList(CDR(body3)));
  588. UNPROTECT_PTR(body3);
  589. setDynamicFlag(VECTOR_ELT(ans, 2), flag3);
  590. flag |= flag3;
  591. }
  592. setAttrib(ans, install("Rd_tag"), header);
  593. UNPROTECT_PTR(header);
  594. setAttrib(ans, R_SrcrefSymbol, makeSrcref(lloc, SrcFile));
  595. setDynamicFlag(ans, flag);
  596. #if DEBUGVALS
  597. Rprintf(" result: %p\n", ans);
  598. #endif
  599. return ans;
  600. }
  601. static void xxsavevalue(SEXP Rd, YYLTYPE *lloc)
  602. {
  603. int flag = getDynamicFlag(Rd);
  604. PROTECT(Value = PairToVectorList(CDR(Rd)));
  605. if (!isNull(Value)) {
  606. setAttrib(Value, R_ClassSymbol, mkString("Rd"));
  607. setAttrib(Value, R_SrcrefSymbol, makeSrcref(lloc, SrcFile));
  608. setDynamicFlag(Value, flag);
  609. }
  610. UNPROTECT_PTR(Rd);
  611. }
  612. static SEXP xxtag(SEXP item, int type, YYLTYPE *lloc)
  613. {
  614. setAttrib(item, install("Rd_tag"), mkString(yytname[YYTRANSLATE(type)]));
  615. setAttrib(item, R_SrcrefSymbol, makeSrcref(lloc, SrcFile));
  616. return item;
  617. }
  618. static void xxWarnNewline()
  619. {
  620. if (xxNewlineInString) {
  621. if(wCalls)
  622. warning(_("newline within quoted string at %s:%d"),
  623. xxBasename, xxNewlineInString);
  624. else
  625. warningcall(R_NilValue,
  626. _("newline within quoted string at %s:%d"),
  627. xxBasename, xxNewlineInString);
  628. }
  629. }
  630. /*----------------------------------------------------------------------------*/
  631. static int (*ptr_getc)(void);
  632. /* Private pushback, since file ungetc only guarantees one byte.
  633. We need arbitrarily large size, since this is how macros are expanded. */
  634. #define PUSH_BACK(c) do { \
  635. if (npush >= pushsize - 1) { \
  636. int *old = pushbase; \
  637. pushsize *= 2; \
  638. pushbase = malloc(pushsize*sizeof(int)); \
  639. if(!pushbase) error(_("unable to allocate buffer for long macro at line %d"), xxlineno);\
  640. memmove(pushbase, old, npush*sizeof(int)); \
  641. if(old != pushback) free(old); } \
  642. pushbase[npush++] = (c); \
  643. } while(0)
  644. #define PUSHBACK_BUFSIZE 32
  645. static int pushback[PUSHBACK_BUFSIZE];
  646. static int *pushbase;
  647. static unsigned int npush, pushsize;
  648. static int macrolevel;
  649. static int prevpos = 0;
  650. static int prevlines[PUSHBACK_BUFSIZE];
  651. static int prevcols[PUSHBACK_BUFSIZE];
  652. static int prevbytes[PUSHBACK_BUFSIZE];
  653. static int xxgetc(void)
  654. {
  655. int c, oldpos;
  656. do {
  657. if(npush) {
  658. c = pushbase[--npush];
  659. if (c == START_MACRO) {
  660. macrolevel++;
  661. if (macrolevel > 1000)
  662. error(_("macros nested too deeply: infinite recursion?"));
  663. } else if (c == END_MACRO) macrolevel--;
  664. } else c = ptr_getc();
  665. } while (c == START_MACRO || c == END_MACRO);
  666. if (!macrolevel) {
  667. oldpos = prevpos;
  668. prevpos = (prevpos + 1) % PUSHBACK_BUFSIZE;
  669. prevbytes[prevpos] = xxbyteno;
  670. prevlines[prevpos] = xxlineno;
  671. /* We only advance the column for the 1st byte in UTF-8, so handle later bytes specially */
  672. if (0x80 <= (unsigned char)c && (unsigned char)c <= 0xBF) {
  673. xxcolno--;
  674. prevcols[prevpos] = prevcols[oldpos];
  675. } else
  676. prevcols[prevpos] = xxcolno;
  677. if (c == EOF) return R_EOF;
  678. R_ParseContextLast = (R_ParseContextLast + 1) % PARSE_CONTEXT_SIZE;
  679. R_ParseContext[R_ParseContextLast] = c;
  680. if (c == '\n') {
  681. xxlineno += 1;
  682. xxcolno = 1;
  683. xxbyteno = 1;
  684. } else {
  685. xxcolno++;
  686. xxbyteno++;
  687. }
  688. if (c == '\t') xxcolno = ((xxcolno + 6) & ~7) + 1;
  689. R_ParseContextLine = xxlineno;
  690. }
  691. /* Rprintf("get %c\n", c); */
  692. return c;
  693. }
  694. static int xxungetc(int c)
  695. {
  696. /* this assumes that c was the result of xxgetc; if not, some edits will be needed */
  697. if (c == END_MACRO) macrolevel++;
  698. if (!macrolevel) {
  699. xxlineno = prevlines[prevpos];
  700. xxbyteno = prevbytes[prevpos];
  701. xxcolno = prevcols[prevpos];
  702. prevpos = (prevpos + PUSHBACK_BUFSIZE - 1) % PUSHBACK_BUFSIZE;
  703. R_ParseContextLine = xxlineno;
  704. R_ParseContext[R_ParseContextLast] = '\0';
  705. /* Mac OS X requires us to keep this non-negative */
  706. R_ParseContextLast = (R_ParseContextLast + PARSE_CONTEXT_SIZE - 1)
  707. % PARSE_CONTEXT_SIZE;
  708. }
  709. if (c == START_MACRO) macrolevel--;
  710. PUSH_BACK(c);
  711. /* Rprintf("unget %c;", c); */
  712. return c;
  713. }
  714. static SEXP makeSrcref(YYLTYPE *lloc, SEXP srcfile)
  715. {
  716. SEXP val;
  717. PROTECT(val = allocVector(INTSXP, 6));
  718. INTEGER(val)[0] = lloc->first_line;
  719. INTEGER(val)[1] = lloc->first_byte;
  720. INTEGER(val)[2] = lloc->last_line;
  721. INTEGER(val)[3] = lloc->last_byte;
  722. INTEGER(val)[4] = lloc->first_column;
  723. INTEGER(val)[5] = lloc->last_column;
  724. setAttrib(val, R_SrcfileSymbol, srcfile);
  725. setAttrib(val, R_ClassSymbol, mkString("srcref"));
  726. UNPROTECT(1);
  727. return val;
  728. }
  729. static SEXP mkString2(const char *s, int len)
  730. {
  731. SEXP t;
  732. cetype_t enc = CE_UTF8;
  733. PROTECT(t = allocVector(STRSXP, 1));
  734. SET_STRING_ELT(t, 0, mkCharLenCE(s, len, enc));
  735. UNPROTECT(1);
  736. return t;
  737. }
  738. /* Stretchy List Structures : Lists are created and grown using a special */
  739. /* dotted pair. The CAR of the list points to the last cons-cell in the */
  740. /* list and the CDR points to the first. The list can be extracted from */
  741. /* the pair by taking its CDR, while the CAR gives fast access to the end */
  742. /* of the list. */
  743. /* Create a stretchy-list dotted pair */
  744. static SEXP NewList(void)
  745. {
  746. SEXP s = CONS(R_NilValue, R_NilValue);
  747. SETCAR(s, s);
  748. return s;
  749. }
  750. /* Add a new element at the end of a stretchy list */
  751. static SEXP GrowList(SEXP l, SEXP s)
  752. {
  753. SEXP tmp;
  754. PROTECT(s);
  755. tmp = CONS(s, R_NilValue);
  756. UNPROTECT(1);
  757. SETCDR(CAR(l), tmp);
  758. SETCAR(l, tmp);
  759. return l;
  760. }
  761. /*--------------------------------------------------------------------------*/
  762. /*
  763. * Parsing Entry Points:
  764. *
  765. * The Following entry points provide Rd parsing facilities.
  766. *
  767. * SEXP R_ParseRd(Rconnection con, ParseStatus *status, SEXP srcfile)
  768. *
  769. */
  770. static SEXP ParseRd(ParseStatus *status, SEXP srcfile, Rboolean fragment)
  771. {
  772. R_ParseContextLast = 0;
  773. R_ParseContext[0] = '\0';
  774. xxlineno = 1;
  775. xxcolno = 1;
  776. xxbyteno = 1;
  777. SrcFile = srcfile;
  778. npush = 0;
  779. pushbase = pushback;
  780. pushsize = PUSHBACK_BUFSIZE;
  781. macrolevel = 0;
  782. xxmode = LATEXLIKE;
  783. xxitemType = UNKNOWN;
  784. xxbraceDepth = 0;
  785. xxinRString = 0;
  786. xxNewlineInString = 0;
  787. xxinEqn = 0;
  788. if (fragment) xxinitvalue = STARTFRAGMENT;
  789. else xxinitvalue = STARTFILE;
  790. xxMacroList = InstallKeywords();
  791. Value = R_NilValue;
  792. if (yyparse()) *status = PARSE_ERROR;
  793. else *status = PARSE_OK;
  794. #if DEBUGVALS
  795. Rprintf("ParseRd result: %p\n", Value);
  796. #endif
  797. UNPROTECT_PTR(Value);
  798. UNPROTECT_PTR(xxMacroList);
  799. if (pushbase != pushback) free(pushbase);
  800. return Value;
  801. }
  802. #include "Rconnections.h"
  803. static Rconnection con_parse;
  804. /* need to handle incomplete last line */
  805. static int con_getc(void)
  806. {
  807. int c;
  808. static int last=-1000;
  809. c = Rconn_fgetc(con_parse);
  810. if (c == EOF && last != '\n') c = '\n';
  811. return (last = c);
  812. }
  813. attribute_hidden
  814. SEXP R_ParseRd(Rconnection con, ParseStatus *status, SEXP srcfile, Rboolean fragment)
  815. {
  816. con_parse = con;
  817. ptr_getc = con_getc;
  818. return ParseRd(status, srcfile, fragment);
  819. }
  820. /*----------------------------------------------------------------------------
  821. *
  822. * The Lexical Analyzer:
  823. *
  824. * Basic lexical analysis is performed by the following
  825. * routines.
  826. *
  827. * The function yylex() scans the input, breaking it into
  828. * tokens which are then passed to the parser.
  829. *
  830. */
  831. /* Special Symbols */
  832. /* Section and R code headers */
  833. struct {
  834. char *name;
  835. int token;
  836. }
  837. /* When adding keywords here, make sure all the handlers
  838. are also modified: checkRd, Rd2HTML, Rd2latex, Rd2txt, any other new ones... */
  839. static keywords[] = {
  840. /* These sections contain Latex-like text */
  841. { "\\author", SECTIONHEADER },
  842. { "\\concept", SECTIONHEADER },
  843. { "\\description",SECTIONHEADER },
  844. { "\\details", SECTIONHEADER },
  845. { "\\docType", SECTIONHEADER },
  846. { "\\encoding",SECTIONHEADER },
  847. { "\\format", SECTIONHEADER },
  848. { "\\keyword", SECTIONHEADER },
  849. { "\\note", SECTIONHEADER },
  850. { "\\references", SECTIONHEADER },
  851. { "\\section", SECTIONHEADER2 },
  852. { "\\seealso", SECTIONHEADER },
  853. { "\\source", SECTIONHEADER },
  854. { "\\title", SECTIONHEADER },
  855. /* These sections contain R-like text */
  856. { "\\examples",RSECTIONHEADER },
  857. { "\\usage", RSECTIONHEADER },
  858. /* These sections contain verbatim text */
  859. { "\\alias", VSECTIONHEADER },
  860. { "\\name", VSECTIONHEADER },
  861. { "\\synopsis",VSECTIONHEADER },
  862. { "\\Rdversion",VSECTIONHEADER },
  863. /* These macros take no arguments. One character non-alpha escapes get the
  864. same token value */
  865. { "\\cr", ESCAPE },
  866. { "\\dots", ESCAPE },
  867. { "\\ldots", ESCAPE },
  868. { "\\R", ESCAPE },
  869. { "\\tab", ESCAPE },
  870. /* These macros take one LaTeX-like argument. */
  871. { "\\acronym", LATEXMACRO },
  872. { "\\bold", LATEXMACRO },
  873. { "\\cite", LATEXMACRO },
  874. { "\\command", LATEXMACRO },
  875. { "\\dfn", LATEXMACRO },
  876. { "\\dQuote", LATEXMACRO },
  877. { "\\email", LATEXMACRO },
  878. { "\\emph", LATEXMACRO },
  879. { "\\file", LATEXMACRO },
  880. { "\\linkS4class", LATEXMACRO },
  881. { "\\pkg", LATEXMACRO },
  882. { "\\sQuote", LATEXMACRO },
  883. { "\\strong", LATEXMACRO },
  884. { "\\var", LATEXMACRO },
  885. /* These are like SECTIONHEADER/LATEXMACRO, but they change the interpretation of \item */
  886. { "\\arguments",LISTSECTION },
  887. { "\\value", LISTSECTION },
  888. { "\\describe",DESCRIPTION },
  889. { "\\enumerate",ITEMIZE },
  890. { "\\itemize", ITEMIZE },
  891. { "\\item", NOITEM }, /* will change to UNKNOWN, ESCAPE, or LATEXMACRO2 depending on context */
  892. /* These macros take two LaTeX-like arguments. */
  893. { "\\enc", LATEXMACRO2 },
  894. { "\\if", LATEXMACRO2 },
  895. { "\\method", LATEXMACRO2 },
  896. { "\\S3method",LATEXMACRO2 },
  897. { "\\S4method",LATEXMACRO2 },
  898. { "\\tabular", LATEXMACRO2 },
  899. { "\\subsection", LATEXMACRO2 },
  900. /* This macro takes one verbatim and one LaTeX-like argument. */
  901. { "\\href", VERBLATEX },
  902. /* This macro takes three LaTeX-like arguments. */
  903. { "\\ifelse", LATEXMACRO3 },
  904. /* These macros take one optional bracketed option and always take
  905. one LaTeX-like argument */
  906. { "\\link", OPTMACRO },
  907. /* These markup macros require an R-like text argument */
  908. { "\\code", RCODEMACRO },
  909. { "\\dontshow",RCODEMACRO },
  910. { "\\donttest",RCODEMACRO },
  911. { "\\testonly",RCODEMACRO },
  912. /* This macro takes one optional bracketed option and one R-like argument */
  913. { "\\Sexpr", SEXPR },
  914. /* This is just like a VSECTIONHEADER, but it needs SEXPR processing */
  915. { "\\RdOpts", RDOPTS },
  916. /* These macros take one verbatim arg and ignore everything except braces */
  917. { "\\dontrun", VERBMACRO }, /* at least for now */
  918. { "\\env", VERBMACRO },
  919. { "\\kbd", VERBMACRO },
  920. { "\\option", VERBMACRO },
  921. { "\\out", VERBMACRO },
  922. { "\\preformatted", VERBMACRO },
  923. { "\\samp", VERBMACRO },
  924. { "\\special", VERBMACRO },
  925. { "\\url", VERBMACRO },
  926. { "\\verb", VERBMACRO },
  927. /* These ones take one or two verbatim args */
  928. { "\\eqn", VERBMACRO2 },
  929. { "\\deqn", VERBMACRO2 },
  930. { "\\figure", VERBMACRO2 },
  931. /* We parse IFDEF/IFNDEF as markup, not as a separate preprocessor step */
  932. { "#ifdef", IFDEF },
  933. { "#ifndef", IFDEF },
  934. { "#endif", ENDIF },
  935. /* These allow user defined macros */
  936. { "\\newcommand", NEWCOMMAND },
  937. { "\\renewcommand", NEWCOMMAND },
  938. { 0, 0 }
  939. /* All other markup macros are rejected. */
  940. };
  941. /* Record the longest # directive here */
  942. #define DIRECTIVE_LEN 7
  943. static SEXP InstallKeywords()
  944. {
  945. int i, num;
  946. SEXP result, name, val;
  947. num = sizeof(keywords)/sizeof(keywords[0]);
  948. PROTECT(result = R_NewHashedEnv(R_EmptyEnv, ScalarInteger(num)));
  949. for (i = 0; keywords[i].name; i++) {
  950. PROTECT(name = install(keywords[i].name));
  951. PROTECT(val = ScalarInteger(keywords[i].token));
  952. defineVar(name, val, result);
  953. UNPROTECT(2);
  954. }
  955. return result;
  956. }
  957. static int KeywordLookup(const char *s)
  958. {
  959. SEXP rec = findVar(install(s), xxMacroList);
  960. if (rec == R_UnboundValue) return UNKNOWN;
  961. else return INTEGER(rec)[0];
  962. }
  963. static SEXP UserMacroLookup(const char *s)
  964. {
  965. SEXP rec = findVar(install(s), xxMacroList);
  966. if (rec == R_UnboundValue) error(_("Unable to find macro %s"), s);
  967. return getAttrib(rec, install("definition"));
  968. }
  969. static void yyerror(const char *s)
  970. {
  971. static const char *const yytname_translations[] =
  972. {
  973. /* the left column are strings coming from bison, the right
  974. column are translations for users.
  975. The first YYENGLISH from the right column are English to be translated,
  976. the rest are to be copied literally. The #if 0 block below allows xgettext
  977. to see these.
  978. */
  979. #define YYENGLISH 17
  980. "$undefined", "input",
  981. "SECTIONHEADER","section header",
  982. "RSECTIONHEADER","section header",
  983. "VSECTIONHEADER","section header",
  984. "LISTSECTION", "section header",
  985. "LATEXMACRO", "macro",
  986. "LATEXMACRO2", "macro",
  987. "LATEXMACRO3", "macro",
  988. "RCODEMACRO", "macro",
  989. "VERBMACRO", "macro",
  990. "VERBMACRO2", "macro",
  991. "ESCAPE", "macro",
  992. "ITEMIZE", "macro",
  993. "IFDEF", "conditional",
  994. "SECTIONHEADER2","section header",
  995. "OPTMACRO", "macro",
  996. "DESCRIPTION", "macro",
  997. "VERB", "VERBATIM TEXT",
  998. 0, 0
  999. };
  1000. static char const yyunexpected[] = "syntax error, unexpected ";
  1001. static char const yyexpecting[] = ", expecting ";
  1002. static char const yyshortunexpected[] = "unexpected %s";
  1003. static char const yylongunexpected[] = "unexpected %s '%s'";
  1004. char *expecting;
  1005. char ParseErrorMsg[PARSE_ERROR_SIZE];
  1006. SEXP filename;
  1007. char ParseErrorFilename[PARSE_ERROR_SIZE];
  1008. #if 0
  1009. /* these are just here to trigger the internationalization */
  1010. _("input");
  1011. _("macro");
  1012. _("conditional");
  1013. _("section header");
  1014. #endif
  1015. xxWarnNewline(); /* post newline warning if necessary */
  1016. /*
  1017. R_ParseError = yylloc.first_line;
  1018. R_ParseErrorCol = yylloc.first_column;
  1019. R_ParseErrorFile = SrcFile;
  1020. */
  1021. if (!strncmp(s, yyunexpected, sizeof yyunexpected -1)) {
  1022. int i, translated = FALSE;
  1023. /* Edit the error message */
  1024. expecting = strstr(s + sizeof yyunexpected -1, yyexpecting);
  1025. if (expecting) *expecting = '\0';
  1026. for (i = 0; yytname_translations[i]; i += 2) {
  1027. if (!strcmp(s + sizeof yyunexpected - 1, yytname_translations[i])) {
  1028. if (yychar < 256)
  1029. sprintf(ParseErrorMsg, _(yyshortunexpected),
  1030. i/2 < YYENGLISH ? _(yytname_translations[i+1])
  1031. : yytname_translations[i+1]);
  1032. else
  1033. sprintf(ParseErrorMsg, _(yylongunexpected),
  1034. i/2 < YYENGLISH ? _(yytname_translations[i+1])
  1035. : yytname_translations[i+1],
  1036. CHAR(STRING_ELT(yylval, 0)));
  1037. translated = TRUE;
  1038. break;
  1039. }
  1040. }
  1041. if (!translated) {
  1042. if (yychar < 256)
  1043. sprintf(ParseErrorMsg, _(yyshortunexpected),
  1044. s + sizeof yyunexpected - 1);
  1045. else
  1046. sprintf(ParseErrorMsg, _(yylongunexpected),
  1047. s + sizeof yyunexpected - 1, CHAR(STRING_ELT(yylval, 0)));
  1048. }
  1049. if (expecting) {
  1050. translated = FALSE;
  1051. for (i = 0; yytname_translations[i]; i += 2) {
  1052. if (!strcmp(expecting + sizeof yyexpecting - 1, yytname_translations[i])) {
  1053. strcat(ParseErrorMsg, _(yyexpecting));
  1054. strcat(ParseErrorMsg, i/2 < YYENGLISH ? _(yytname_translations[i+1])
  1055. : yytname_translations[i+1]);
  1056. translated = TRUE;
  1057. break;
  1058. }
  1059. }
  1060. if (!translated) {
  1061. strcat(ParseErrorMsg, _(yyexpecting));
  1062. strcat(ParseErrorMsg, expecting + sizeof yyexpecting - 1);
  1063. }
  1064. }
  1065. } else if (!strncmp(s, yyunknown, sizeof yyunknown-1)) {
  1066. sprintf(ParseErrorMsg, "%s '%s'", s, CHAR(STRING_ELT(yylval, 0)));
  1067. } else {
  1068. sprintf(ParseErrorMsg, "%s", s);
  1069. }
  1070. filename = findVar(install("filename"), SrcFile);
  1071. if (isString(filename) && length(filename))
  1072. strncpy(ParseErrorFilename, CHAR(STRING_ELT(filename, 0)), PARSE_ERROR_SIZE - 1);
  1073. else
  1074. ParseErrorFilename[0] = '\0';
  1075. if (wCalls) {
  1076. if (yylloc.first_line != yylloc.last_line)
  1077. warning("%s:%d-%d: %s",
  1078. ParseErrorFilename, yylloc.first_line, yylloc.last_line, ParseErrorMsg);
  1079. else
  1080. warning("%s:%d: %s",
  1081. ParseErrorFilename, yylloc.first_line, ParseErrorMsg);
  1082. } else {
  1083. if (yylloc.first_line != yylloc.last_line)
  1084. warningcall(R_NilValue, "%s:%d-%d: %s",
  1085. ParseErrorFilename, yylloc.first_line, yylloc.last_line, ParseErrorMsg);
  1086. else
  1087. warningcall(R_NilValue, "%s:%d: %s",
  1088. ParseErrorFilename, yylloc.first_line, ParseErrorMsg);
  1089. }
  1090. }
  1091. #define TEXT_PUSH(c) do { \
  1092. unsigned int nc = bp - stext; \
  1093. if (nc >= nstext - 1) { \
  1094. char *old = stext; \
  1095. nstext *= 2; \
  1096. stext = malloc(nstext); \
  1097. if(!stext) error(_("unable to allocate buffer for long string at line %d"), xxlineno);\
  1098. memmove(stext, old, nc); \
  1099. if(old != st0) free(old); \
  1100. bp = stext+nc; } \
  1101. *bp++ = (c); \
  1102. } while(0)
  1103. static void setfirstloc(void)
  1104. {
  1105. yylloc.first_line = xxlineno;
  1106. yylloc.first_column = xxcolno;
  1107. yylloc.first_byte = xxbyteno;
  1108. }
  1109. static void setlastloc(void)
  1110. {
  1111. yylloc.last_line = prevlines[prevpos];
  1112. yylloc.last_column = prevcols[prevpos];
  1113. yylloc.last_byte = prevbytes[prevpos];
  1114. }
  1115. /* Split the input stream into tokens. */
  1116. /* This is the lowest of the parsing levels. */
  1117. static int token(void)
  1118. {
  1119. int c, lookahead;
  1120. int outsideLiteral = xxmode == LATEXLIKE || xxmode == INOPTION || xxbraceDepth == 0;
  1121. if (xxinitvalue) {
  1122. yylloc.first_line = 0;
  1123. yylloc.first_column = 0;
  1124. yylloc.first_byte = 0;
  1125. yylloc.last_line = 0;
  1126. yylloc.last_column = 0;
  1127. yylloc.last_byte = 0;
  1128. PROTECT(yylval = mkString(""));
  1129. c = xxinitvalue;
  1130. xxinitvalue = 0;
  1131. return(c);
  1132. }
  1133. setfirstloc();
  1134. c = xxgetc();
  1135. switch (c) {
  1136. case '%': if (!xxinEqn) return mkComment(c);
  1137. break;
  1138. case '\\':
  1139. if (!xxinEqn) {
  1140. lookahead = xxungetc(xxgetc());
  1141. if (isalpha(lookahead) && xxmode != VERBATIM
  1142. /* In R strings, only link or var is allowed as markup */
  1143. && (lookahead == 'l' || lookahead == 'v' || !xxinRString))
  1144. return mkMarkup(c);
  1145. }
  1146. break;
  1147. case R_EOF:
  1148. if (xxinRString) {
  1149. xxWarnNewline();
  1150. error(_("Unexpected end of input (in %c quoted string opened at %s:%d:%d)"),
  1151. xxinRString, xxBasename, xxQuoteLine, xxQuoteCol);
  1152. }
  1153. return END_OF_INPUT;
  1154. case '#':
  1155. if (!xxinEqn && yylloc.first_column == 1) return mkIfdef(c);
  1156. break;
  1157. case LBRACE:
  1158. if (!xxinRString) {
  1159. xxbraceDepth++;
  1160. if (outsideLiteral) return c;
  1161. }
  1162. break;
  1163. case RBRACE:
  1164. if (!xxinRString) {
  1165. xxbraceDepth--;
  1166. if (outsideLiteral || xxbraceDepth == 0) return c;
  1167. }
  1168. break;
  1169. case '[':
  1170. case ']':
  1171. if (xxmode == INOPTION ) return c;
  1172. break;
  1173. }
  1174. switch (xxmode) {
  1175. case RLIKE: return mkCode(c);
  1176. case INOPTION:
  1177. case LATEXLIKE: return mkText(c);
  1178. case VERBATIM: return mkVerb(c);
  1179. }
  1180. return ERROR; /* We shouldn't get here. */
  1181. }
  1182. #define INITBUFSIZE 128
  1183. static int mkText(int c)
  1184. {
  1185. char st0[INITBUFSIZE];
  1186. unsigned int nstext = INITBUFSIZE;
  1187. char *stext = st0, *bp = st0, lookahead;
  1188. while(1) {
  1189. switch (c) {
  1190. case '\\':
  1191. lookahead = xxgetc();
  1192. if (lookahead == LBRACE || lookahead == RBRACE ||
  1193. lookahead == '%' || lookahead == '\\') {
  1194. c = lookahead;
  1195. break;
  1196. }
  1197. xxungetc(lookahead);
  1198. if (isalpha(lookahead)) goto stop;
  1199. case ']':
  1200. if (xxmode == INOPTION) goto stop;
  1201. break;
  1202. case '%':
  1203. case LBRACE:
  1204. case RBRACE:
  1205. case R_EOF:
  1206. goto stop;
  1207. }
  1208. TEXT_PUSH(c);
  1209. if (c == '\n') goto stop;
  1210. c = xxgetc();
  1211. };
  1212. stop:
  1213. if (c != '\n') xxungetc(c); /* newline causes a break, but we keep it */
  1214. PROTECT(yylval = mkString2(stext, bp - stext));
  1215. if(stext != st0) free(stext);
  1216. return TEXT;
  1217. }
  1218. static int mkComment(int c)
  1219. {
  1220. char st0[INITBUFSIZE];
  1221. unsigned int nstext = INITBUFSIZE;
  1222. char *stext = st0, *bp = st0;
  1223. do TEXT_PUSH(c);
  1224. while ((c = xxgetc()) != '\n' && c != R_EOF);
  1225. xxungetc(c);
  1226. PROTECT(yylval = mkString2(stext, bp - stext));
  1227. if(stext != st0) free(stext);
  1228. return COMMENT;
  1229. }
  1230. static int mkCode(int c)
  1231. {
  1232. char st0[INITBUFSIZE];
  1233. unsigned int nstext = INITBUFSIZE;
  1234. char *stext = st0, *bp = st0;
  1235. /* Avoid double counting initial braces */
  1236. if (c == LBRACE && !xxinRString) xxbraceDepth--;
  1237. if (c == RBRACE && !xxinRString) xxbraceDepth++;
  1238. while(1) {
  1239. int escaped = 0;
  1240. if (c == '\\') {
  1241. int lookahead = xxgetc();
  1242. if (lookahead == '\\' || lookahead == '%') {
  1243. c = lookahead;
  1244. escaped = 1;
  1245. } else xxungetc(lookahead);
  1246. }
  1247. if ((!escaped && c == '%') || c == R_EOF) break;
  1248. if (xxinRString) {
  1249. /* This stuff is messy, because there are two levels of escaping:
  1250. The Rd escaping and the R code string escaping. */
  1251. if (c == '\\') {
  1252. int lookahead = xxgetc();
  1253. if (lookahead == '\\') { /* This must be the 3rd backslash */
  1254. lookahead = xxgetc();
  1255. if (lookahead == xxinRString || lookahead == '\\') {
  1256. TEXT_PUSH(c);
  1257. c = lookahead;
  1258. escaped = 1;
  1259. } else {
  1260. xxungetc(lookahead); /* put back the 4th char */
  1261. xxungetc('\\'); /* and the 3rd */
  1262. }
  1263. } else if (lookahead == xxinRString) { /* There could be one or two before this */
  1264. TEXT_PUSH(c);
  1265. c = lookahead;
  1266. escaped = 1;
  1267. } else if (!escaped && (lookahead == 'l' || lookahead == 'v')) {
  1268. /* assume \link or \var; this breaks vertical tab, but does anyone ever use that? */
  1269. xxungetc(lookahead);
  1270. break;
  1271. } else xxungetc(lookahead);
  1272. }
  1273. if (!escaped && c == xxinRString)
  1274. xxinRString = 0;
  1275. } else {
  1276. if (c == '#') {
  1277. do {
  1278. int escaped = 0;
  1279. TEXT_PUSH(c);
  1280. c = xxgetc();
  1281. if (c == '\\') {
  1282. int lookahead = xxgetc();
  1283. if (lookahead == '\\' || lookahead == '%' || lookahead == LBRACE || lookahead == RBRACE) {
  1284. c = lookahead;
  1285. escaped = 1;
  1286. } else xxungetc(lookahead);
  1287. }
  1288. if (c == LBRACE && !escaped) xxbraceDepth++;
  1289. else if (c == RBRACE && !escaped) xxbraceDepth--;
  1290. } while (c != '\n' && c != R_EOF && xxbraceDepth > 0);
  1291. if (c == RBRACE && !escaped) xxbraceDepth++; /* avoid double counting */
  1292. }
  1293. if (c == '\'' || c == '"' || c == '`') {
  1294. xxinRString = c;
  1295. xxQuoteLine = xxlineno;
  1296. xxQuoteCol = xxcolno;
  1297. } else if (c == '\\' && !escaped) {
  1298. int lookahead = xxgetc();
  1299. if (lookahead == LBRACE || lookahead == RBRACE) {
  1300. c = lookahead;
  1301. } else if (isalpha(lookahead)) {
  1302. xxungetc(lookahead);
  1303. c = '\\';
  1304. break;
  1305. } else {
  1306. TEXT_PUSH('\\');
  1307. c = lookahead;
  1308. }
  1309. } else if (c == LBRACE) {
  1310. xxbraceDepth++;
  1311. } else if (c == RBRACE) {
  1312. if (xxbraceDepth == 1) break;
  1313. else xxbraceDepth--;
  1314. } else if (c == R_EOF) break;
  1315. }
  1316. TEXT_PUSH(c);
  1317. if (c == '\n') {
  1318. if (xxinRString && !xxNewlineInString)
  1319. xxNewlineInString = xxlineno-1;
  1320. break;
  1321. }
  1322. c = xxgetc();
  1323. }
  1324. if (c != '\n') xxungetc(c);
  1325. PROTECT(yylval = mkString2(stext, bp - stext));
  1326. if(stext != st0) free(stext);
  1327. return RCODE;
  1328. }
  1329. static int mkMarkup(int c)
  1330. {
  1331. char st0[INITBUFSIZE];
  1332. unsigned int nstext = INITBUFSIZE;
  1333. char *stext = st0, *bp = st0;
  1334. int retval = 0, attempt = 0;
  1335. TEXT_PUSH(c);
  1336. while (isalnum((c = xxgetc()))) TEXT_PUSH(c);
  1337. while (attempt++ < 2) {
  1338. /* character escapes are processed as text, not markup */
  1339. if (bp == stext+1) {
  1340. TEXT_PUSH(c);
  1341. TEXT_PUSH('\0');
  1342. retval = TEXT;
  1343. c = xxgetc();
  1344. break;
  1345. } else {
  1346. TEXT_PUSH('\0');
  1347. retval = KeywordLookup(stext);
  1348. if (retval == UNKNOWN && attempt == 1) { /* try again, non-digit…

Large files files are truncated, but you can click here to view the full file