Debian Codegraph @

vignettes/highr-internals.Rmd

raw
1 <!--
2 %\VignetteEngine{knitr::docco_classic}
3 %\VignetteIndexEntry{Internals of the highr package}
4 -->
5
6 # Internals of the `highr` package
7
8 The **highr** package is based on the function `getParseData()`, which was
9 introduced in R 3.0.0. This function gives detailed information of the
10 symbols in a code fragment. A simple example:
11
12 ```{r}
13 p = parse(text = " xx = 1 + 1 # a comment", keep.source = TRUE)
14 (d = getParseData(p))
15 ```
16
17 The first step is to filter out the rows that we do not need:
18
19 ```{r}
20 (d = d[d$terminal, ])
21 ```
22
23 There is a column `token` in the data frame, and we will wrap this column
24 with markup commands, e.g. `\hlnum{1}` for the numeric constant `1`. We
25 defined the markup commands in `cmd_latex` and `cmd_html`:
26
27 ```{r}
28 head(highr:::cmd_latex)
29 tail(highr:::cmd_html)
30 ```
31
32 These command data frames are connected to the tokens in the R code via
33 their row names:
34
35 ```{r}
36 d$token
37 rownames(highr:::cmd_latex)
38 ```
39
40 Now we know how to wrap up the R tokens. The next big question is how to
41 restore the white spaces in the source code, since they were not directly
42 available in the parsed data, but the parsed data contains column numbers,
43 and we can derive the positions of white spaces from them. For example,
44 `col2 = 5` for the first row, and `col1 = 7` for the next row, and that
45 indicates there must be one space after the token in the first row, otherwise
46 the next row will start at the position `6` instead of `7`.
47
48 A small trick is used to fill in the gaps of white spaces:
49
50 ```{r}
51 (z = d[, c('col1', 'col2')]) # take out the column positions
52 (z = t(z)) # transpose the matrix
53 (z = c(z)) # turn it into a vector
54 (z = c(0, head(z, -1))) # append 0 in the beginning, and remove the last element
55 (z = matrix(z, ncol = 2, byrow = TRUE))
56 ```
57
58 Now the two columns indicate the starting and ending positions of spaces,
59 and we can easily figure out how many white spaces are needed for each row:
60
61 ```{r}
62 (s = z[, 2] - z[, 1] - 1)
63 (s = strrep(' ', s))
64 paste(s, d$text, sep = '')
65 ```
66
67 So we have successfully restored the white spaces in the source code. Let's
68 paste all pieces together (suppose we highlight for LaTeX):
69
70 ```{r}
71 m = highr:::cmd_latex[d$token, ]
72 cbind(d, m)
73 # use standard markup if tokens do not exist in the table
74 m[is.na(m[, 1]), ] = highr:::cmd_latex['DEFAULT', ]
75 paste(s, m[, 1], d$text, m[, 2], sep = '', collapse = '')
76 ```
77
78 So far so simple. That is one line of code, after all. A next challenge
79 comes when there are multiple lines, and a token spans across multiple lines:
80
81 ```{r}
82 d = getParseData(parse(text = "x = \"a character\nstring\" #hi", keep.source = TRUE))
83 (d = d[d$terminal, ])
84 ```
85
86 Take a look at the third row. It says that the character string starts from
87 line 1, and ends on line 2. In this case, we just pretend as if everything
88 on line 1 were on line 2. Then for each line, we append the missing spaces
89 and apply markup commands to text symbols.
90
91 ```{r}
92 d$line1[d$line1 == 1] = 2
93 d
94 ```
95
96 Do not worry about the column `line2`. It does not matter. Only `line1` is
97 needed to indicate the line number here.
98
99 Why do we need to highlight line by line instead of applying highlighting
100 commands to all text symbols (a.k.a vectorization)? Well, the margin of this
101 paper is too small to write down the answer.