parser.js 14 KB

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