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

/Rexample/rdev/src/library/tools/src/gramRd.y

https://bitbucket.org/pengyuut/rexample
Happy | 1921 lines | 1650 code | 271 blank | 0 comment | 0 complexity | 4749240ead881d25eed20311d7dd64c1 MD5 | raw file

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

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