-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathparse.js
153 lines (132 loc) · 4.22 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* @import {Element, Root} from 'hast'
* @import {IGrammar, StateStack} from 'vscode-textmate'
*/
import {classes, grandparents, transparent} from './theme.js'
// Source: <https://github.com/microsoft/vscode-textmate/blob/9157c7f/src/metadata.ts#L33-L35>
// Also: <https://github.com/microsoft/vscode/blob/8ca37ee/src/vs/editor/common/encodedTokenAttributes.ts#L71>
const FONT_STYLE_MASK = 0b0000_0000_0000_0000_0111_1000_0000_0000
const FOREGROUND_MASK = 0b0000_0000_1111_1111_1000_0000_0000_0000
const BACKGROUND_MASK = 0b1111_1111_0000_0000_0000_0000_0000_0000
// Source: <https://github.com/microsoft/vscode-textmate/blob/9157c7f/src/metadata.ts#L37-L42>
// Also: <https://github.com/microsoft/vscode/blob/8ca37ee/src/vs/editor/common/encodedTokenAttributes.ts#L92-L94>
const FONT_STYLE_OFFSET = 11
const FOREGROUND_OFFSET = 15
const BACKGROUND_OFFSET = 24
/**
* @param {string} value
* Code to highlight.
* @param {Readonly<IGrammar>} grammar
* Grammar instance to use.
* @param {ReadonlyArray<string>} colors
* Colors that are in the theme.
* @returns {Root}
* A hast root that includes basic `<span>`s and text nodes.
*/
export function parse(value, grammar, colors) {
/** @type {Root} */
const tree = {type: 'root', children: []}
const search = /\r?\n|\r/g
/** @type {StateStack | null} */
let stack = null
let start = 0
while (start < value.length) {
const match = search.exec(value)
const end = match ? match.index : value.length
// > 👉 **Important**: empty lines have to be tokenized, as some patterns
// > look for them.
const {ruleStack, tokens} = grammar.tokenizeLine2(
value.slice(start, end),
stack
)
let index = 0
while (index < tokens.length) {
const tokenStart = start + tokens[index++]
const metadata = tokens[index++]
const tokenEnd = index < tokens.length ? start + tokens[index] : end
// Source: <https://github.com/microsoft/vscode-textmate/blob/9157c7f/src/metadata.ts#L71-L93>
const fg = (metadata & FOREGROUND_MASK) >>> FOREGROUND_OFFSET
const bg = (metadata & BACKGROUND_MASK) >>> BACKGROUND_OFFSET
const fs = (metadata & FONT_STYLE_MASK) >>> FONT_STYLE_OFFSET
/** @type {Element | Root} */
let scope = tree
scope = delveIfClassName(scope, fontStyleToClass(fs))
scope = delveIfClassName(scope, colorToClass(colors[bg]))
scope = delveIfClassName(scope, colorToClass(colors[fg]))
appendText(scope, value.slice(tokenStart, tokenEnd))
}
stack = ruleStack
start = end
if (match) {
appendText(tree, match[0])
start += match[0].length
}
}
return tree
}
/**
* @param {Element | Root} scope
* Node to work in.
* @param {string | undefined} className
* Class to add.
* @returns {Element | Root}
* The new element, if with `className`, or the given `scope`.
*/
function delveIfClassName(scope, className) {
if (!className) return scope
let tail = scope.children[scope.children.length - 1]
if (
!tail ||
tail.type !== 'element' ||
!tail.properties ||
!Array.isArray(tail.properties.className) ||
!tail.properties.className.includes(className)
) {
tail = {
type: 'element',
tagName: 'span',
properties: {className: [className]},
children: []
}
scope.children.push(tail)
}
return tail
}
/**
* @param {Element | Root} scope
* Node to work in.
* @param {string} value
* Text to add.
* @returns {undefined}
* Nothing.
*/
function appendText(scope, value) {
let tail = scope.children[scope.children.length - 1]
if (!tail || tail.type !== 'text') {
tail = {type: 'text', value: ''}
scope.children.push(tail)
}
tail.value += value
}
/**
* Note: there’s only one grandparent.
* We could encode more grandparents in `fontStyle` if needed.
*
* @param {number} fontStyle
* Font style to convert..
* @returns {string | undefined}
* Class name.
*/
function fontStyleToClass(fontStyle) {
return fontStyle ? grandparents[0] : undefined
}
/**
* @param {string} color
* Color to convert.
* @returns {string | undefined}
* Class name.
*/
function colorToClass(color) {
if (color === transparent) return undefined
return classes[Number.parseInt(color.slice(1), 10)]
}