You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

503 lines
8.8 KiB

  1. module.exports = (function(){function _waka(parser, startRule) {
  2. if(startRule && ! parser.rules[startRule])
  3. throw new Error('start rule missing: ' + JSON.stringify(startRule))
  4. return {
  5. getState: function() {
  6. return parser.state
  7. },
  8. getTrace: function(message) {
  9. return (message ? message + '\n' : '') + parser.state.traceLine()
  10. },
  11. exec: function(input) {
  12. if(! startRule)
  13. throw new Error('no start rule given')
  14. parser.state.setInput(input)
  15. try {
  16. var value = parser.rules[startRule]()
  17. }
  18. catch(err) {
  19. var error = err
  20. }
  21. if(error == null) {
  22. if(! parser.state.adv || ! parser.state.isEOF())
  23. var error = new Error('Unexpected syntax in top')
  24. }
  25. return {
  26. success: error == null,
  27. value: ! error ? value : undefined,
  28. error: error
  29. }
  30. },
  31. startWith: function(rule) {
  32. return _waka(parser, rule)
  33. },
  34. }
  35. };
  36. return _waka((function(){'use strict';
  37. var _rules={};
  38. _rules.NameStartChar = function() {
  39. var _R=_P.match(":");
  40. if(!_P.adv){ _P.adv=true;
  41. var $0=_P.cur();
  42. if($0==null){_P.adv=false;
  43. var _R=null;
  44. }else{
  45. var _R=_P.step("A"<=$0&&$0<="Z");
  46. }
  47. }
  48. if(!_P.adv){ _P.adv=true;
  49. var _R=_P.match("_");
  50. }
  51. if(!_P.adv){ _P.adv=true;
  52. var $1=_P.cur();
  53. if($1==null){_P.adv=false;
  54. var _R=null;
  55. }else{
  56. var _R=_P.step("a"<=$1&&$1<="z");
  57. }
  58. }
  59. if(!_P.adv){ _P.adv=true;
  60. var $2=_P.cur();
  61. if($2==null){_P.adv=false;
  62. var _R=null;
  63. }else{
  64. var _R=_P.step("\u00C0"<=$2&&$2<="\u00D6");
  65. }
  66. }
  67. if(!_P.adv){ _P.adv=true;
  68. var $3=_P.cur();
  69. if($3==null){_P.adv=false;
  70. var _R=null;
  71. }else{
  72. var _R=_P.step("\u00D8"<=$3&&$3<="\u00F6");
  73. }
  74. }
  75. if(!_P.adv){ _P.adv=true;
  76. var $4=_P.cur();
  77. if($4==null){_P.adv=false;
  78. var _R=null;
  79. }else{
  80. var _R=_P.step("\u00F8"<=$4&&$4<="\u02FF");
  81. }
  82. }
  83. if(!_P.adv){ _P.adv=true;
  84. var $5=_P.cur();
  85. if($5==null){_P.adv=false;
  86. var _R=null;
  87. }else{
  88. var _R=_P.step("\u0370"<=$5&&$5<="\u037D");
  89. }
  90. }
  91. if(!_P.adv){ _P.adv=true;
  92. var $6=_P.cur();
  93. if($6==null){_P.adv=false;
  94. var _R=null;
  95. }else{
  96. var _R=_P.step("\u037F"<=$6&&$6<="\u1FFF");
  97. }
  98. }
  99. if(!_P.adv){ _P.adv=true;
  100. var $7=_P.cur();
  101. if($7==null){_P.adv=false;
  102. var _R=null;
  103. }else{
  104. var _R=_P.step("\u200C"<=$7&&$7<="\u200D");
  105. }
  106. }
  107. if(!_P.adv){ _P.adv=true;
  108. var $8=_P.cur();
  109. if($8==null){_P.adv=false;
  110. var _R=null;
  111. }else{
  112. var _R=_P.step("\u2070"<=$8&&$8<="\u218F");
  113. }
  114. }
  115. if(!_P.adv){ _P.adv=true;
  116. var $9=_P.cur();
  117. if($9==null){_P.adv=false;
  118. var _R=null;
  119. }else{
  120. var _R=_P.step("\u2C00"<=$9&&$9<="\u2FEF");
  121. }
  122. }
  123. if(!_P.adv){ _P.adv=true;
  124. var $a=_P.cur();
  125. if($a==null){_P.adv=false;
  126. var _R=null;
  127. }else{
  128. var _R=_P.step("\u3001"<=$a&&$a<="\uD7FF");
  129. }
  130. }
  131. if(!_P.adv){ _P.adv=true;
  132. var $b=_P.cur();
  133. if($b==null){_P.adv=false;
  134. var _R=null;
  135. }else{
  136. var _R=_P.step("\uF900"<=$b&&$b<="\uFDCF");
  137. }
  138. }
  139. if(!_P.adv){ _P.adv=true;
  140. var $c=_P.cur();
  141. if($c==null){_P.adv=false;
  142. var _R=null;
  143. }else{
  144. var _R=_P.step("\uFDF0"<=$c&&$c<="\uFFFD");
  145. }
  146. }
  147. if(!_P.adv){ _P.adv=true;
  148. $d:{var $e=_P.pos;
  149. var $f=_P.cur();
  150. if($f==null){_P.adv=false;
  151. null;
  152. }else{
  153. _P.step("\uD800"<=$f&&$f<="\uDB7F");
  154. }
  155. if(!_P.adv) break $d;
  156. var $g=_P.cur();
  157. if($g==null){_P.adv=false;
  158. null;
  159. }else{
  160. _P.step("\uDC00"<=$g&&$g<="\uDFFF");
  161. }
  162. var _R=_P.doc.substring($e,_P.pos);
  163. }
  164. if(!_P.adv) _P.pos=$e;
  165. }
  166. return _R;
  167. }
  168. _rules.NameChar = function() {
  169. var _R=_rules.NameStartChar();
  170. if(!_P.adv){ _P.adv=true;
  171. var _R=_P.match("-");
  172. }
  173. if(!_P.adv){ _P.adv=true;
  174. var _R=_P.match(".");
  175. }
  176. if(!_P.adv){ _P.adv=true;
  177. var $0=_P.cur();
  178. if($0==null){_P.adv=false;
  179. var _R=null;
  180. }else{
  181. var _R=_P.step("0"<=$0&&$0<="9");
  182. }
  183. }
  184. if(!_P.adv){ _P.adv=true;
  185. var _R=_P.match("\u00B7");
  186. }
  187. if(!_P.adv){ _P.adv=true;
  188. var $1=_P.cur();
  189. if($1==null){_P.adv=false;
  190. var _R=null;
  191. }else{
  192. var _R=_P.step("\u0300"<=$1&&$1<="\u036F");
  193. }
  194. }
  195. if(!_P.adv){ _P.adv=true;
  196. var $2=_P.cur();
  197. if($2==null){_P.adv=false;
  198. var _R=null;
  199. }else{
  200. var _R=_P.step("\u203F"<=$2&&$2<="\u2040");
  201. }
  202. }
  203. return _R;
  204. }
  205. _rules.Name = function() {
  206. $0:{var $1=_P.pos;
  207. _rules.NameStartChar();
  208. if(!_P.adv) break $0;
  209. var $2=false;
  210. for(;;) {
  211. _rules.NameChar();
  212. if(!_P.adv) break;
  213. $2=true;
  214. }; _P.adv=true;
  215. var _R=_P.doc.substring($1,_P.pos);
  216. }
  217. if(!_P.adv) _P.pos=$1;
  218. return _R;
  219. }
  220. _rules.QName = function() {
  221. var _R=_rules.PrefixedName();
  222. if(!_P.adv){ _P.adv=true;
  223. var _R=_rules.UnprefixedName();
  224. }
  225. return _R;
  226. }
  227. _rules.PrefixedName = function() {
  228. $0:{var $1=_P.pos;
  229. _rules.Prefix();
  230. if(!_P.adv) break $0;
  231. _P.match(":");
  232. if(!_P.adv) break $0;
  233. _rules.LocalPart();
  234. var _R=_P.doc.substring($1,_P.pos);
  235. }
  236. if(!_P.adv) _P.pos=$1;
  237. return _R;
  238. }
  239. _rules.UnprefixedName = function() {
  240. var _R=_rules.LocalPart();
  241. return _R;
  242. }
  243. _rules.Prefix = function() {
  244. var _R=_rules.NCName();
  245. return _R;
  246. }
  247. _rules.LocalPart = function() {
  248. var _R=_rules.NCName();
  249. return _R;
  250. }
  251. _rules.NCNameStartChar = function() {
  252. var $0=_P.cur();
  253. if($0==null){_P.adv=false;
  254. var _R=null;
  255. }else{
  256. var _R=_P.step("A"<=$0&&$0<="Z");
  257. }
  258. if(!_P.adv){ _P.adv=true;
  259. var _R=_P.match("_");
  260. }
  261. if(!_P.adv){ _P.adv=true;
  262. var $1=_P.cur();
  263. if($1==null){_P.adv=false;
  264. var _R=null;
  265. }else{
  266. var _R=_P.step("a"<=$1&&$1<="z");
  267. }
  268. }
  269. if(!_P.adv){ _P.adv=true;
  270. var $2=_P.cur();
  271. if($2==null){_P.adv=false;
  272. var _R=null;
  273. }else{
  274. var _R=_P.step("\u00C0"<=$2&&$2<="\u00D6");
  275. }
  276. }
  277. if(!_P.adv){ _P.adv=true;
  278. var $3=_P.cur();
  279. if($3==null){_P.adv=false;
  280. var _R=null;
  281. }else{
  282. var _R=_P.step("\u00D8"<=$3&&$3<="\u00F6");
  283. }
  284. }
  285. if(!_P.adv){ _P.adv=true;
  286. var $4=_P.cur();
  287. if($4==null){_P.adv=false;
  288. var _R=null;
  289. }else{
  290. var _R=_P.step("\u00F8"<=$4&&$4<="\u02FF");
  291. }
  292. }
  293. if(!_P.adv){ _P.adv=true;
  294. var $5=_P.cur();
  295. if($5==null){_P.adv=false;
  296. var _R=null;
  297. }else{
  298. var _R=_P.step("\u0370"<=$5&&$5<="\u037D");
  299. }
  300. }
  301. if(!_P.adv){ _P.adv=true;
  302. var $6=_P.cur();
  303. if($6==null){_P.adv=false;
  304. var _R=null;
  305. }else{
  306. var _R=_P.step("\u037F"<=$6&&$6<="\u1FFF");
  307. }
  308. }
  309. if(!_P.adv){ _P.adv=true;
  310. var $7=_P.cur();
  311. if($7==null){_P.adv=false;
  312. var _R=null;
  313. }else{
  314. var _R=_P.step("\u200C"<=$7&&$7<="\u200D");
  315. }
  316. }
  317. if(!_P.adv){ _P.adv=true;
  318. var $8=_P.cur();
  319. if($8==null){_P.adv=false;
  320. var _R=null;
  321. }else{
  322. var _R=_P.step("\u2070"<=$8&&$8<="\u218F");
  323. }
  324. }
  325. if(!_P.adv){ _P.adv=true;
  326. var $9=_P.cur();
  327. if($9==null){_P.adv=false;
  328. var _R=null;
  329. }else{
  330. var _R=_P.step("\u2C00"<=$9&&$9<="\u2FEF");
  331. }
  332. }
  333. if(!_P.adv){ _P.adv=true;
  334. var $a=_P.cur();
  335. if($a==null){_P.adv=false;
  336. var _R=null;
  337. }else{
  338. var _R=_P.step("\u3001"<=$a&&$a<="\uD7FF");
  339. }
  340. }
  341. if(!_P.adv){ _P.adv=true;
  342. var $b=_P.cur();
  343. if($b==null){_P.adv=false;
  344. var _R=null;
  345. }else{
  346. var _R=_P.step("\uF900"<=$b&&$b<="\uFDCF");
  347. }
  348. }
  349. if(!_P.adv){ _P.adv=true;
  350. var $c=_P.cur();
  351. if($c==null){_P.adv=false;
  352. var _R=null;
  353. }else{
  354. var _R=_P.step("\uFDF0"<=$c&&$c<="\uFFFD");
  355. }
  356. }
  357. if(!_P.adv){ _P.adv=true;
  358. $d:{var $e=_P.pos;
  359. var $f=_P.cur();
  360. if($f==null){_P.adv=false;
  361. null;
  362. }else{
  363. _P.step("\uD800"<=$f&&$f<="\uDB7F");
  364. }
  365. if(!_P.adv) break $d;
  366. var $g=_P.cur();
  367. if($g==null){_P.adv=false;
  368. null;
  369. }else{
  370. _P.step("\uDC00"<=$g&&$g<="\uDFFF");
  371. }
  372. var _R=_P.doc.substring($e,_P.pos);
  373. }
  374. if(!_P.adv) _P.pos=$e;
  375. }
  376. return _R;
  377. }
  378. _rules.NCNameChar = function() {
  379. var _R=_rules.NCNameStartChar();
  380. if(!_P.adv){ _P.adv=true;
  381. var _R=_P.match("-");
  382. }
  383. if(!_P.adv){ _P.adv=true;
  384. var _R=_P.match(".");
  385. }
  386. if(!_P.adv){ _P.adv=true;
  387. var $0=_P.cur();
  388. if($0==null){_P.adv=false;
  389. var _R=null;
  390. }else{
  391. var _R=_P.step("0"<=$0&&$0<="9");
  392. }
  393. }
  394. if(!_P.adv){ _P.adv=true;
  395. var _R=_P.match("\u00B7");
  396. }
  397. if(!_P.adv){ _P.adv=true;
  398. var $1=_P.cur();
  399. if($1==null){_P.adv=false;
  400. var _R=null;
  401. }else{
  402. var _R=_P.step("\u0300"<=$1&&$1<="\u036F");
  403. }
  404. }
  405. if(!_P.adv){ _P.adv=true;
  406. var $2=_P.cur();
  407. if($2==null){_P.adv=false;
  408. var _R=null;
  409. }else{
  410. var _R=_P.step("\u203F"<=$2&&$2<="\u2040");
  411. }
  412. }
  413. return _R;
  414. }
  415. _rules.NCName = function() {
  416. $0:{var $1=_P.pos;
  417. _rules.NCNameStartChar();
  418. if(!_P.adv) break $0;
  419. var $2=false;
  420. for(;;) {
  421. _rules.NCNameChar();
  422. if(!_P.adv) break;
  423. $2=true;
  424. }; _P.adv=true;
  425. var _R=_P.doc.substring($1,_P.pos);
  426. }
  427. if(!_P.adv) _P.pos=$1;
  428. return _R;
  429. }
  430. function ParserState() {
  431. this.doc = ''
  432. this.pos = 0
  433. this.adv = true
  434. this.setInput = function(doc) {
  435. this.doc = doc
  436. this.pos = 0
  437. this.adv = true
  438. }
  439. this.isEOF = function() {
  440. return this.pos == this.doc.length
  441. }
  442. this.cur = function() {
  443. return _P.doc[_P.pos]
  444. }
  445. this.match = function(str) {
  446. if(_P.adv = _P.doc.substr(_P.pos, str.length) == str) {
  447. _P.pos += str.length
  448. return str
  449. }
  450. }
  451. this.step = function(flag) {
  452. if(_P.adv = flag) {
  453. _P.pos++
  454. return _P.doc[_P.pos - 1]
  455. }
  456. }
  457. this.unexpected = function(rule) {
  458. throw new Error('Unexpected syntax in ' + rule)
  459. }
  460. this.traceLine = function(pos) {
  461. if(! pos) pos = _P.pos
  462. var from = _P.doc.lastIndexOf('\n', pos), to = _P.doc.indexOf('\n', pos)
  463. if(from == -1)
  464. from = 0
  465. else
  466. from++
  467. if(to == -1)
  468. to = pos.length
  469. var lineNo = _P.doc.substring(0, from).split('\n').length
  470. var line = _P.doc.substring(from, to)
  471. var pointer = Array(200).join(' ').substr(0, pos - from) + '^^^'
  472. return (
  473. 'Line ' + lineNo + ':\n' +
  474. line + '\n' +
  475. pointer
  476. )
  477. }
  478. }
  479. var _P = new ParserState
  480. return {
  481. state: _P,
  482. rules: _rules,
  483. }
  484. })(),null)})()