a zip code crypto-currency system good for red ONLY

parse.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 'use strict'
  2. // The ABNF grammar in the spec is totally ambiguous.
  3. //
  4. // This parser follows the operator precedence defined in the
  5. // `Order of Precedence and Parentheses` section.
  6. module.exports = function (tokens) {
  7. var index = 0
  8. function hasMore () {
  9. return index < tokens.length
  10. }
  11. function token () {
  12. return hasMore() ? tokens[index] : null
  13. }
  14. function next () {
  15. if (!hasMore()) {
  16. throw new Error()
  17. }
  18. index++
  19. }
  20. function parseOperator (operator) {
  21. var t = token()
  22. if (t && t.type === 'OPERATOR' && operator === t.string) {
  23. next()
  24. return t.string
  25. }
  26. }
  27. function parseWith () {
  28. if (parseOperator('WITH')) {
  29. var t = token()
  30. if (t && t.type === 'EXCEPTION') {
  31. next()
  32. return t.string
  33. }
  34. throw new Error('Expected exception after `WITH`')
  35. }
  36. }
  37. function parseLicenseRef () {
  38. // TODO: Actually, everything is concatenated into one string
  39. // for backward-compatibility but it could be better to return
  40. // a nice structure.
  41. var begin = index
  42. var string = ''
  43. var t = token()
  44. if (t.type === 'DOCUMENTREF') {
  45. next()
  46. string += 'DocumentRef-' + t.string + ':'
  47. if (!parseOperator(':')) {
  48. throw new Error('Expected `:` after `DocumentRef-...`')
  49. }
  50. }
  51. t = token()
  52. if (t.type === 'LICENSEREF') {
  53. next()
  54. string += 'LicenseRef-' + t.string
  55. return {license: string}
  56. }
  57. index = begin
  58. }
  59. function parseLicense () {
  60. var t = token()
  61. if (t && t.type === 'LICENSE') {
  62. next()
  63. var node = {license: t.string}
  64. if (parseOperator('+')) {
  65. node.plus = true
  66. }
  67. var exception = parseWith()
  68. if (exception) {
  69. node.exception = exception
  70. }
  71. return node
  72. }
  73. }
  74. function parseParenthesizedExpression () {
  75. var left = parseOperator('(')
  76. if (!left) {
  77. return
  78. }
  79. var expr = parseExpression()
  80. if (!parseOperator(')')) {
  81. throw new Error('Expected `)`')
  82. }
  83. return expr
  84. }
  85. function parseAtom () {
  86. return (
  87. parseParenthesizedExpression() ||
  88. parseLicenseRef() ||
  89. parseLicense()
  90. )
  91. }
  92. function makeBinaryOpParser (operator, nextParser) {
  93. return function parseBinaryOp () {
  94. var left = nextParser()
  95. if (!left) {
  96. return
  97. }
  98. if (!parseOperator(operator)) {
  99. return left
  100. }
  101. var right = parseBinaryOp()
  102. if (!right) {
  103. throw new Error('Expected expression')
  104. }
  105. return {
  106. left: left,
  107. conjunction: operator.toLowerCase(),
  108. right: right
  109. }
  110. }
  111. }
  112. var parseAnd = makeBinaryOpParser('AND', parseAtom)
  113. var parseExpression = makeBinaryOpParser('OR', parseAnd)
  114. var node = parseExpression()
  115. if (!node || hasMore()) {
  116. throw new Error('Syntax error')
  117. }
  118. return node
  119. }