parser.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. 'use strict'
  2. let Declaration = require('./declaration')
  3. let tokenizer = require('./tokenize')
  4. let Comment = require('./comment')
  5. let AtRule = require('./at-rule')
  6. let Root = require('./root')
  7. let Rule = require('./rule')
  8. const SAFE_COMMENT_NEIGHBOR = {
  9. empty: true,
  10. space: true
  11. }
  12. function findLastWithPosition(tokens) {
  13. for (let i = tokens.length - 1; i >= 0; i--) {
  14. let token = tokens[i]
  15. let pos = token[3] || token[2]
  16. if (pos) return pos
  17. }
  18. }
  19. class Parser {
  20. constructor(input) {
  21. this.input = input
  22. this.root = new Root()
  23. this.current = this.root
  24. this.spaces = ''
  25. this.semicolon = false
  26. this.customProperty = false
  27. this.createTokenizer()
  28. this.root.source = { input, start: { column: 1, line: 1, offset: 0 } }
  29. }
  30. atrule(token) {
  31. let node = new AtRule()
  32. node.name = token[1].slice(1)
  33. if (node.name === '') {
  34. this.unnamedAtrule(node, token)
  35. }
  36. this.init(node, token[2])
  37. let type
  38. let prev
  39. let shift
  40. let last = false
  41. let open = false
  42. let params = []
  43. let brackets = []
  44. while (!this.tokenizer.endOfFile()) {
  45. token = this.tokenizer.nextToken()
  46. type = token[0]
  47. if (type === '(' || type === '[') {
  48. brackets.push(type === '(' ? ')' : ']')
  49. } else if (type === '{' && brackets.length > 0) {
  50. brackets.push('}')
  51. } else if (type === brackets[brackets.length - 1]) {
  52. brackets.pop()
  53. }
  54. if (brackets.length === 0) {
  55. if (type === ';') {
  56. node.source.end = this.getPosition(token[2])
  57. node.source.end.offset++
  58. this.semicolon = true
  59. break
  60. } else if (type === '{') {
  61. open = true
  62. break
  63. } else if (type === '}') {
  64. if (params.length > 0) {
  65. shift = params.length - 1
  66. prev = params[shift]
  67. while (prev && prev[0] === 'space') {
  68. prev = params[--shift]
  69. }
  70. if (prev) {
  71. node.source.end = this.getPosition(prev[3] || prev[2])
  72. node.source.end.offset++
  73. }
  74. }
  75. this.end(token)
  76. break
  77. } else {
  78. params.push(token)
  79. }
  80. } else {
  81. params.push(token)
  82. }
  83. if (this.tokenizer.endOfFile()) {
  84. last = true
  85. break
  86. }
  87. }
  88. node.raws.between = this.spacesAndCommentsFromEnd(params)
  89. if (params.length) {
  90. node.raws.afterName = this.spacesAndCommentsFromStart(params)
  91. this.raw(node, 'params', params)
  92. if (last) {
  93. token = params[params.length - 1]
  94. node.source.end = this.getPosition(token[3] || token[2])
  95. node.source.end.offset++
  96. this.spaces = node.raws.between
  97. node.raws.between = ''
  98. }
  99. } else {
  100. node.raws.afterName = ''
  101. node.params = ''
  102. }
  103. if (open) {
  104. node.nodes = []
  105. this.current = node
  106. }
  107. }
  108. checkMissedSemicolon(tokens) {
  109. let colon = this.colon(tokens)
  110. if (colon === false) return
  111. let founded = 0
  112. let token
  113. for (let j = colon - 1; j >= 0; j--) {
  114. token = tokens[j]
  115. if (token[0] !== 'space') {
  116. founded += 1
  117. if (founded === 2) break
  118. }
  119. }
  120. // If the token is a word, e.g. `!important`, `red` or any other valid property's value.
  121. // Then we need to return the colon after that word token. [3] is the "end" colon of that word.
  122. // And because we need it after that one we do +1 to get the next one.
  123. throw this.input.error(
  124. 'Missed semicolon',
  125. token[0] === 'word' ? token[3] + 1 : token[2]
  126. )
  127. }
  128. colon(tokens) {
  129. let brackets = 0
  130. let token, type, prev
  131. for (let [i, element] of tokens.entries()) {
  132. token = element
  133. type = token[0]
  134. if (type === '(') {
  135. brackets += 1
  136. }
  137. if (type === ')') {
  138. brackets -= 1
  139. }
  140. if (brackets === 0 && type === ':') {
  141. if (!prev) {
  142. this.doubleColon(token)
  143. } else if (prev[0] === 'word' && prev[1] === 'progid') {
  144. continue
  145. } else {
  146. return i
  147. }
  148. }
  149. prev = token
  150. }
  151. return false
  152. }
  153. comment(token) {
  154. let node = new Comment()
  155. this.init(node, token[2])
  156. node.source.end = this.getPosition(token[3] || token[2])
  157. node.source.end.offset++
  158. let text = token[1].slice(2, -2)
  159. if (/^\s*$/.test(text)) {
  160. node.text = ''
  161. node.raws.left = text
  162. node.raws.right = ''
  163. } else {
  164. let match = text.match(/^(\s*)([^]*\S)(\s*)$/)
  165. node.text = match[2]
  166. node.raws.left = match[1]
  167. node.raws.right = match[3]
  168. }
  169. }
  170. createTokenizer() {
  171. this.tokenizer = tokenizer(this.input)
  172. }
  173. decl(tokens, customProperty) {
  174. let node = new Declaration()
  175. this.init(node, tokens[0][2])
  176. let last = tokens[tokens.length - 1]
  177. if (last[0] === ';') {
  178. this.semicolon = true
  179. tokens.pop()
  180. }
  181. node.source.end = this.getPosition(
  182. last[3] || last[2] || findLastWithPosition(tokens)
  183. )
  184. node.source.end.offset++
  185. while (tokens[0][0] !== 'word') {
  186. if (tokens.length === 1) this.unknownWord(tokens)
  187. node.raws.before += tokens.shift()[1]
  188. }
  189. node.source.start = this.getPosition(tokens[0][2])
  190. node.prop = ''
  191. while (tokens.length) {
  192. let type = tokens[0][0]
  193. if (type === ':' || type === 'space' || type === 'comment') {
  194. break
  195. }
  196. node.prop += tokens.shift()[1]
  197. }
  198. node.raws.between = ''
  199. let token
  200. while (tokens.length) {
  201. token = tokens.shift()
  202. if (token[0] === ':') {
  203. node.raws.between += token[1]
  204. break
  205. } else {
  206. if (token[0] === 'word' && /\w/.test(token[1])) {
  207. this.unknownWord([token])
  208. }
  209. node.raws.between += token[1]
  210. }
  211. }
  212. if (node.prop[0] === '_' || node.prop[0] === '*') {
  213. node.raws.before += node.prop[0]
  214. node.prop = node.prop.slice(1)
  215. }
  216. let firstSpaces = []
  217. let next
  218. while (tokens.length) {
  219. next = tokens[0][0]
  220. if (next !== 'space' && next !== 'comment') break
  221. firstSpaces.push(tokens.shift())
  222. }
  223. this.precheckMissedSemicolon(tokens)
  224. for (let i = tokens.length - 1; i >= 0; i--) {
  225. token = tokens[i]
  226. if (token[1].toLowerCase() === '!important') {
  227. node.important = true
  228. let string = this.stringFrom(tokens, i)
  229. string = this.spacesFromEnd(tokens) + string
  230. if (string !== ' !important') node.raws.important = string
  231. break
  232. } else if (token[1].toLowerCase() === 'important') {
  233. let cache = tokens.slice(0)
  234. let str = ''
  235. for (let j = i; j > 0; j--) {
  236. let type = cache[j][0]
  237. if (str.trim().indexOf('!') === 0 && type !== 'space') {
  238. break
  239. }
  240. str = cache.pop()[1] + str
  241. }
  242. if (str.trim().indexOf('!') === 0) {
  243. node.important = true
  244. node.raws.important = str
  245. tokens = cache
  246. }
  247. }
  248. if (token[0] !== 'space' && token[0] !== 'comment') {
  249. break
  250. }
  251. }
  252. let hasWord = tokens.some(i => i[0] !== 'space' && i[0] !== 'comment')
  253. if (hasWord) {
  254. node.raws.between += firstSpaces.map(i => i[1]).join('')
  255. firstSpaces = []
  256. }
  257. this.raw(node, 'value', firstSpaces.concat(tokens), customProperty)
  258. if (node.value.includes(':') && !customProperty) {
  259. this.checkMissedSemicolon(tokens)
  260. }
  261. }
  262. doubleColon(token) {
  263. throw this.input.error(
  264. 'Double colon',
  265. { offset: token[2] },
  266. { offset: token[2] + token[1].length }
  267. )
  268. }
  269. emptyRule(token) {
  270. let node = new Rule()
  271. this.init(node, token[2])
  272. node.selector = ''
  273. node.raws.between = ''
  274. this.current = node
  275. }
  276. end(token) {
  277. if (this.current.nodes && this.current.nodes.length) {
  278. this.current.raws.semicolon = this.semicolon
  279. }
  280. this.semicolon = false
  281. this.current.raws.after = (this.current.raws.after || '') + this.spaces
  282. this.spaces = ''
  283. if (this.current.parent) {
  284. this.current.source.end = this.getPosition(token[2])
  285. this.current.source.end.offset++
  286. this.current = this.current.parent
  287. } else {
  288. this.unexpectedClose(token)
  289. }
  290. }
  291. endFile() {
  292. if (this.current.parent) this.unclosedBlock()
  293. if (this.current.nodes && this.current.nodes.length) {
  294. this.current.raws.semicolon = this.semicolon
  295. }
  296. this.current.raws.after = (this.current.raws.after || '') + this.spaces
  297. this.root.source.end = this.getPosition(this.tokenizer.position())
  298. }
  299. freeSemicolon(token) {
  300. this.spaces += token[1]
  301. if (this.current.nodes) {
  302. let prev = this.current.nodes[this.current.nodes.length - 1]
  303. if (prev && prev.type === 'rule' && !prev.raws.ownSemicolon) {
  304. prev.raws.ownSemicolon = this.spaces
  305. this.spaces = ''
  306. }
  307. }
  308. }
  309. // Helpers
  310. getPosition(offset) {
  311. let pos = this.input.fromOffset(offset)
  312. return {
  313. column: pos.col,
  314. line: pos.line,
  315. offset
  316. }
  317. }
  318. init(node, offset) {
  319. this.current.push(node)
  320. node.source = {
  321. input: this.input,
  322. start: this.getPosition(offset)
  323. }
  324. node.raws.before = this.spaces
  325. this.spaces = ''
  326. if (node.type !== 'comment') this.semicolon = false
  327. }
  328. other(start) {
  329. let end = false
  330. let type = null
  331. let colon = false
  332. let bracket = null
  333. let brackets = []
  334. let customProperty = start[1].startsWith('--')
  335. let tokens = []
  336. let token = start
  337. while (token) {
  338. type = token[0]
  339. tokens.push(token)
  340. if (type === '(' || type === '[') {
  341. if (!bracket) bracket = token
  342. brackets.push(type === '(' ? ')' : ']')
  343. } else if (customProperty && colon && type === '{') {
  344. if (!bracket) bracket = token
  345. brackets.push('}')
  346. } else if (brackets.length === 0) {
  347. if (type === ';') {
  348. if (colon) {
  349. this.decl(tokens, customProperty)
  350. return
  351. } else {
  352. break
  353. }
  354. } else if (type === '{') {
  355. this.rule(tokens)
  356. return
  357. } else if (type === '}') {
  358. this.tokenizer.back(tokens.pop())
  359. end = true
  360. break
  361. } else if (type === ':') {
  362. colon = true
  363. }
  364. } else if (type === brackets[brackets.length - 1]) {
  365. brackets.pop()
  366. if (brackets.length === 0) bracket = null
  367. }
  368. token = this.tokenizer.nextToken()
  369. }
  370. if (this.tokenizer.endOfFile()) end = true
  371. if (brackets.length > 0) this.unclosedBracket(bracket)
  372. if (end && colon) {
  373. if (!customProperty) {
  374. while (tokens.length) {
  375. token = tokens[tokens.length - 1][0]
  376. if (token !== 'space' && token !== 'comment') break
  377. this.tokenizer.back(tokens.pop())
  378. }
  379. }
  380. this.decl(tokens, customProperty)
  381. } else {
  382. this.unknownWord(tokens)
  383. }
  384. }
  385. parse() {
  386. let token
  387. while (!this.tokenizer.endOfFile()) {
  388. token = this.tokenizer.nextToken()
  389. switch (token[0]) {
  390. case 'space':
  391. this.spaces += token[1]
  392. break
  393. case ';':
  394. this.freeSemicolon(token)
  395. break
  396. case '}':
  397. this.end(token)
  398. break
  399. case 'comment':
  400. this.comment(token)
  401. break
  402. case 'at-word':
  403. this.atrule(token)
  404. break
  405. case '{':
  406. this.emptyRule(token)
  407. break
  408. default:
  409. this.other(token)
  410. break
  411. }
  412. }
  413. this.endFile()
  414. }
  415. precheckMissedSemicolon(/* tokens */) {
  416. // Hook for Safe Parser
  417. }
  418. raw(node, prop, tokens, customProperty) {
  419. let token, type
  420. let length = tokens.length
  421. let value = ''
  422. let clean = true
  423. let next, prev
  424. for (let i = 0; i < length; i += 1) {
  425. token = tokens[i]
  426. type = token[0]
  427. if (type === 'space' && i === length - 1 && !customProperty) {
  428. clean = false
  429. } else if (type === 'comment') {
  430. prev = tokens[i - 1] ? tokens[i - 1][0] : 'empty'
  431. next = tokens[i + 1] ? tokens[i + 1][0] : 'empty'
  432. if (!SAFE_COMMENT_NEIGHBOR[prev] && !SAFE_COMMENT_NEIGHBOR[next]) {
  433. if (value.slice(-1) === ',') {
  434. clean = false
  435. } else {
  436. value += token[1]
  437. }
  438. } else {
  439. clean = false
  440. }
  441. } else {
  442. value += token[1]
  443. }
  444. }
  445. if (!clean) {
  446. let raw = tokens.reduce((all, i) => all + i[1], '')
  447. node.raws[prop] = { raw, value }
  448. }
  449. node[prop] = value
  450. }
  451. rule(tokens) {
  452. tokens.pop()
  453. let node = new Rule()
  454. this.init(node, tokens[0][2])
  455. node.raws.between = this.spacesAndCommentsFromEnd(tokens)
  456. this.raw(node, 'selector', tokens)
  457. this.current = node
  458. }
  459. spacesAndCommentsFromEnd(tokens) {
  460. let lastTokenType
  461. let spaces = ''
  462. while (tokens.length) {
  463. lastTokenType = tokens[tokens.length - 1][0]
  464. if (lastTokenType !== 'space' && lastTokenType !== 'comment') break
  465. spaces = tokens.pop()[1] + spaces
  466. }
  467. return spaces
  468. }
  469. // Errors
  470. spacesAndCommentsFromStart(tokens) {
  471. let next
  472. let spaces = ''
  473. while (tokens.length) {
  474. next = tokens[0][0]
  475. if (next !== 'space' && next !== 'comment') break
  476. spaces += tokens.shift()[1]
  477. }
  478. return spaces
  479. }
  480. spacesFromEnd(tokens) {
  481. let lastTokenType
  482. let spaces = ''
  483. while (tokens.length) {
  484. lastTokenType = tokens[tokens.length - 1][0]
  485. if (lastTokenType !== 'space') break
  486. spaces = tokens.pop()[1] + spaces
  487. }
  488. return spaces
  489. }
  490. stringFrom(tokens, from) {
  491. let result = ''
  492. for (let i = from; i < tokens.length; i++) {
  493. result += tokens[i][1]
  494. }
  495. tokens.splice(from, tokens.length - from)
  496. return result
  497. }
  498. unclosedBlock() {
  499. let pos = this.current.source.start
  500. throw this.input.error('Unclosed block', pos.line, pos.column)
  501. }
  502. unclosedBracket(bracket) {
  503. throw this.input.error(
  504. 'Unclosed bracket',
  505. { offset: bracket[2] },
  506. { offset: bracket[2] + 1 }
  507. )
  508. }
  509. unexpectedClose(token) {
  510. throw this.input.error(
  511. 'Unexpected }',
  512. { offset: token[2] },
  513. { offset: token[2] + 1 }
  514. )
  515. }
  516. unknownWord(tokens) {
  517. throw this.input.error(
  518. 'Unknown word',
  519. { offset: tokens[0][2] },
  520. { offset: tokens[0][2] + tokens[0][1].length }
  521. )
  522. }
  523. unnamedAtrule(node, token) {
  524. throw this.input.error(
  525. 'At-rule without name',
  526. { offset: token[2] },
  527. { offset: token[2] + token[1].length }
  528. )
  529. }
  530. }
  531. module.exports = Parser