parser.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. package xregex
  2. /*
  3. golang version regex parser
  4. refer to: https://github.com/aristotle9/as3cc/tree/master/java-template/src/org/lala/lex/utils/parser
  5. */
  6. import (
  7. "container/list"
  8. "fmt"
  9. "strings"
  10. )
  11. // Parser golang regex parser
  12. type Parser struct {
  13. actionTable []map[int64]int64
  14. gotoTable map[int64]map[int64]int64
  15. prodList []*productionItem
  16. inputTable map[string]int64
  17. codes []interface{}
  18. duplCount int64
  19. num int64
  20. flags string
  21. flagsC string
  22. }
  23. // Info print parser info.
  24. func (p *Parser) Info() (s string) {
  25. s += fmt.Sprintf("actionTable len:%d,", len(p.actionTable))
  26. s += fmt.Sprintf("prodList len:%d,", len(p.prodList))
  27. s += fmt.Sprintf("inputTable len:%d,", len(p.inputTable))
  28. s += fmt.Sprintf("codes len:%d\n", len(p.codes))
  29. return
  30. }
  31. // New return regex parser instance.
  32. func New() (p *Parser) {
  33. p = &Parser{}
  34. var tmp map[int64]int64
  35. p.actionTable = make([]map[int64]int64, 0)
  36. p.actionTable = append(p.actionTable, nil)
  37. tmp = make(map[int64]int64)
  38. tmp[0x20] = 0x4
  39. tmp[0x21] = 0x18
  40. tmp[0x4] = 0x2E
  41. tmp[0x5] = 0x30
  42. tmp[0x6] = 0x1
  43. tmp[0x7] = 0x2
  44. tmp[0x8] = 0x1C
  45. tmp[0x2A] = 0x24
  46. tmp[0x2B] = 0x20
  47. tmp[0x24] = 0x1E
  48. tmp[0x27] = 0x6
  49. tmp[0x10] = 0x10
  50. tmp[0x11] = 0x12
  51. tmp[0x12] = 0x14
  52. tmp[0x14] = 0x1A
  53. tmp[0x15] = 0xE
  54. tmp[0x16] = 0x8
  55. tmp[0x17] = 0xC
  56. tmp[0x1A] = 0x16
  57. tmp[0x1D] = 0xA
  58. tmp[0x2C] = 0x22
  59. p.actionTable = append(p.actionTable, tmp)
  60. tmp = make(map[int64]int64)
  61. tmp[0x0] = 0x5
  62. tmp[0x21] = 0x18
  63. tmp[0x4] = 0x2E
  64. tmp[0x5] = 0x30
  65. tmp[0x2C] = 0x22
  66. tmp[0x8] = 0x1C
  67. tmp[0x9] = 0x2F
  68. tmp[0x2A] = 0x24
  69. tmp[0x2B] = 0x20
  70. tmp[0x24] = 0x1E
  71. tmp[0x10] = 0x10
  72. tmp[0x11] = 0x12
  73. tmp[0x12] = 0x14
  74. tmp[0x14] = 0x1A
  75. tmp[0x15] = 0xE
  76. tmp[0x17] = 0xC
  77. tmp[0x1A] = 0x16
  78. tmp[0x1D] = 0xA
  79. p.actionTable = append(p.actionTable, tmp)
  80. tmp = make(map[int64]int64)
  81. tmp[0x21] = 0x18
  82. tmp[0x4] = 0x2E
  83. tmp[0x5] = 0x30
  84. tmp[0x7] = 0x21
  85. tmp[0x8] = 0x1C
  86. tmp[0x9] = 0x21
  87. tmp[0xA] = 0x21
  88. tmp[0x2B] = 0x20
  89. tmp[0x24] = 0x1E
  90. tmp[0x10] = 0x10
  91. tmp[0x11] = 0x12
  92. tmp[0x12] = 0x14
  93. tmp[0x14] = 0x1A
  94. tmp[0x15] = 0xE
  95. tmp[0x17] = 0xC
  96. tmp[0x1A] = 0x16
  97. tmp[0x2A] = 0x24
  98. tmp[0x1D] = 0xA
  99. tmp[0x2C] = 0x22
  100. p.actionTable = append(p.actionTable, tmp)
  101. tmp = make(map[int64]int64)
  102. tmp[0x0] = 0x7
  103. tmp[0x1] = 0x7
  104. tmp[0x2] = 0x7
  105. tmp[0x4] = 0x2E
  106. tmp[0x5] = 0x30
  107. tmp[0x7] = 0x7
  108. tmp[0x8] = 0x1C
  109. tmp[0x9] = 0x7
  110. tmp[0xA] = 0x7
  111. tmp[0x2B] = 0x20
  112. tmp[0x24] = 0x1E
  113. tmp[0xF] = 0x7
  114. tmp[0x10] = 0x10
  115. tmp[0x11] = 0x12
  116. tmp[0x12] = 0x14
  117. tmp[0x14] = 0x1A
  118. tmp[0x15] = 0xE
  119. tmp[0x17] = 0xC
  120. tmp[0x1A] = 0x16
  121. tmp[0x2A] = 0x24
  122. tmp[0x21] = 0x18
  123. tmp[0x1D] = 0x7
  124. tmp[0x2C] = 0x22
  125. p.actionTable = append(p.actionTable, tmp)
  126. tmp = make(map[int64]int64)
  127. tmp[0x21] = 0x18
  128. tmp[0x4] = 0x2E
  129. tmp[0x5] = 0x30
  130. tmp[0x2C] = 0x22
  131. tmp[0x8] = 0x1C
  132. tmp[0xA] = 0x31
  133. tmp[0x2B] = 0x20
  134. tmp[0x24] = 0x1E
  135. tmp[0x10] = 0x10
  136. tmp[0x11] = 0x12
  137. tmp[0x12] = 0x14
  138. tmp[0x14] = 0x1A
  139. tmp[0x15] = 0xE
  140. tmp[0x17] = 0xC
  141. tmp[0x1A] = 0x16
  142. tmp[0x2A] = 0x24
  143. tmp[0x1D] = 0xA
  144. p.actionTable = append(p.actionTable, tmp)
  145. tmp = make(map[int64]int64)
  146. tmp[0x21] = 0x18
  147. tmp[0x4] = 0x2E
  148. tmp[0x5] = 0x30
  149. tmp[0x7] = 0x23
  150. tmp[0x8] = 0x1C
  151. tmp[0x9] = 0x23
  152. tmp[0xA] = 0x23
  153. tmp[0x2B] = 0x20
  154. tmp[0x24] = 0x1E
  155. tmp[0x10] = 0x10
  156. tmp[0x11] = 0x12
  157. tmp[0x12] = 0x14
  158. tmp[0x14] = 0x23
  159. tmp[0x15] = 0xE
  160. tmp[0x17] = 0xC
  161. tmp[0x1A] = 0x16
  162. tmp[0x2A] = 0x24
  163. tmp[0x1D] = 0x23
  164. tmp[0x2C] = 0x22
  165. p.actionTable = append(p.actionTable, tmp)
  166. tmp = make(map[int64]int64)
  167. tmp[0x21] = 0x18
  168. tmp[0x4] = 0x2E
  169. tmp[0x5] = 0x30
  170. tmp[0x7] = 0x25
  171. tmp[0x8] = 0x1C
  172. tmp[0x9] = 0x25
  173. tmp[0xA] = 0x25
  174. tmp[0x2B] = 0x20
  175. tmp[0x24] = 0x1E
  176. tmp[0x10] = 0x10
  177. tmp[0x11] = 0x12
  178. tmp[0x12] = 0x14
  179. tmp[0x14] = 0x25
  180. tmp[0x15] = 0xE
  181. tmp[0x17] = 0xC
  182. tmp[0x1A] = 0x16
  183. tmp[0x2A] = 0x24
  184. tmp[0x1D] = 0x25
  185. tmp[0x2C] = 0x22
  186. p.actionTable = append(p.actionTable, tmp)
  187. tmp = make(map[int64]int64)
  188. tmp[0x21] = 0x18
  189. tmp[0x4] = 0x2E
  190. tmp[0x5] = 0x30
  191. tmp[0x7] = 0x27
  192. tmp[0x8] = 0x1C
  193. tmp[0x9] = 0x27
  194. tmp[0xA] = 0x27
  195. tmp[0x2B] = 0x20
  196. tmp[0x24] = 0x1E
  197. tmp[0x10] = 0x10
  198. tmp[0x11] = 0x12
  199. tmp[0x12] = 0x14
  200. tmp[0x14] = 0x27
  201. tmp[0x15] = 0xE
  202. tmp[0x17] = 0xC
  203. tmp[0x1A] = 0x16
  204. tmp[0x2A] = 0x24
  205. tmp[0x1D] = 0x27
  206. tmp[0x2C] = 0x22
  207. p.actionTable = append(p.actionTable, tmp)
  208. tmp = make(map[int64]int64)
  209. tmp[0x0] = 0x9
  210. tmp[0x1] = 0x9
  211. tmp[0x2] = 0x9
  212. tmp[0x4] = 0x2E
  213. tmp[0x5] = 0x30
  214. tmp[0x7] = 0x9
  215. tmp[0x8] = 0x1C
  216. tmp[0x9] = 0x9
  217. tmp[0xA] = 0x9
  218. tmp[0x2B] = 0x20
  219. tmp[0x24] = 0x1E
  220. tmp[0xF] = 0x9
  221. tmp[0x10] = 0x10
  222. tmp[0x11] = 0x12
  223. tmp[0x12] = 0x14
  224. tmp[0x14] = 0x1A
  225. tmp[0x15] = 0xE
  226. tmp[0x17] = 0xC
  227. tmp[0x1A] = 0x16
  228. tmp[0x2A] = 0x24
  229. tmp[0x21] = 0x18
  230. tmp[0x1D] = 0x9
  231. tmp[0x2C] = 0x22
  232. p.actionTable = append(p.actionTable, tmp)
  233. tmp = make(map[int64]int64)
  234. tmp[0x28] = 0x26
  235. tmp[0x22] = 0x2C
  236. tmp[0x1C] = 0x28
  237. tmp[0x5] = 0x30
  238. tmp[0x18] = 0x45
  239. tmp[0x1B] = 0x2E
  240. tmp[0xC] = 0x2E
  241. tmp[0xD] = 0x2A
  242. tmp[0xE] = 0x37
  243. p.actionTable = append(p.actionTable, tmp)
  244. tmp = make(map[int64]int64)
  245. tmp[0x3] = 0x19
  246. p.actionTable = append(p.actionTable, tmp)
  247. tmp = make(map[int64]int64)
  248. tmp[0x21] = 0x18
  249. tmp[0x4] = 0x2E
  250. tmp[0x5] = 0x30
  251. tmp[0x7] = 0x29
  252. tmp[0x8] = 0x1C
  253. tmp[0x9] = 0x29
  254. tmp[0xA] = 0x29
  255. tmp[0x2B] = 0x20
  256. tmp[0x24] = 0x1E
  257. tmp[0x10] = 0x10
  258. tmp[0x11] = 0x12
  259. tmp[0x12] = 0x14
  260. tmp[0x14] = 0x29
  261. tmp[0x15] = 0xE
  262. tmp[0x17] = 0xC
  263. tmp[0x1A] = 0x16
  264. tmp[0x2A] = 0x24
  265. tmp[0x1D] = 0x29
  266. tmp[0x2C] = 0x22
  267. p.actionTable = append(p.actionTable, tmp)
  268. tmp = make(map[int64]int64)
  269. tmp[0x1F] = 0x4F
  270. tmp[0x25] = 0x55
  271. tmp[0x13] = 0x3F
  272. p.actionTable = append(p.actionTable, tmp)
  273. tmp = make(map[int64]int64)
  274. tmp[0x29] = 0x5B
  275. tmp[0x25] = 0x57
  276. tmp[0x1E] = 0x4B
  277. tmp[0x26] = 0x59
  278. p.actionTable = append(p.actionTable, tmp)
  279. tmp = make(map[int64]int64)
  280. tmp[0x1E] = 0x4D
  281. tmp[0x13] = 0x41
  282. p.actionTable = append(p.actionTable, tmp)
  283. tmp = make(map[int64]int64)
  284. tmp[0x0] = 0xB
  285. tmp[0x1] = 0xB
  286. tmp[0x2] = 0xB
  287. tmp[0x3] = 0x1B
  288. tmp[0x4] = 0x2E
  289. tmp[0x5] = 0x30
  290. tmp[0x7] = 0xB
  291. tmp[0x8] = 0x1C
  292. tmp[0x9] = 0xB
  293. tmp[0xA] = 0xB
  294. tmp[0xB] = 0x1B
  295. tmp[0xC] = 0x2E
  296. tmp[0xD] = 0x2A
  297. tmp[0xE] = 0x39
  298. tmp[0xF] = 0xB
  299. tmp[0x10] = 0x10
  300. tmp[0x11] = 0x12
  301. tmp[0x12] = 0x14
  302. tmp[0x14] = 0x1A
  303. tmp[0x15] = 0xE
  304. tmp[0x16] = 0x8
  305. tmp[0x17] = 0xC
  306. tmp[0x18] = 0x39
  307. tmp[0x19] = 0x47
  308. tmp[0x1A] = 0x16
  309. tmp[0x1B] = 0x2E
  310. tmp[0x1C] = 0x28
  311. tmp[0x1D] = 0xB
  312. tmp[0x20] = 0xB
  313. tmp[0x21] = 0x18
  314. tmp[0x22] = 0x2C
  315. tmp[0x23] = 0x53
  316. tmp[0x24] = 0x1E
  317. tmp[0x27] = 0x6
  318. tmp[0x28] = 0x26
  319. tmp[0x2A] = 0x24
  320. tmp[0x2B] = 0x20
  321. tmp[0x2C] = 0x22
  322. p.actionTable = append(p.actionTable, tmp)
  323. tmp = make(map[int64]int64)
  324. tmp[0xC] = 0x35
  325. tmp[0x1B] = 0x49
  326. p.actionTable = append(p.actionTable, tmp)
  327. tmp = make(map[int64]int64)
  328. tmp[0x0] = 0xD
  329. tmp[0x1] = 0xD
  330. tmp[0x2] = 0xD
  331. tmp[0x3] = 0xD
  332. tmp[0x4] = 0x2E
  333. tmp[0x5] = 0x30
  334. tmp[0x7] = 0xD
  335. tmp[0x8] = 0x1C
  336. tmp[0x9] = 0xD
  337. tmp[0xA] = 0xD
  338. tmp[0xB] = 0xD
  339. tmp[0xC] = 0x2E
  340. tmp[0xD] = 0x2A
  341. tmp[0xE] = 0xD
  342. tmp[0xF] = 0xD
  343. tmp[0x10] = 0x10
  344. tmp[0x11] = 0x12
  345. tmp[0x12] = 0x14
  346. tmp[0x14] = 0x1A
  347. tmp[0x15] = 0xE
  348. tmp[0x16] = 0x8
  349. tmp[0x17] = 0xC
  350. tmp[0x18] = 0xD
  351. tmp[0x1A] = 0x16
  352. tmp[0x1B] = 0x2E
  353. tmp[0x1C] = 0x28
  354. tmp[0x1D] = 0xD
  355. tmp[0x20] = 0xD
  356. tmp[0x21] = 0x18
  357. tmp[0x22] = 0x2C
  358. tmp[0x24] = 0x1E
  359. tmp[0x27] = 0x6
  360. tmp[0x28] = 0x26
  361. tmp[0x2A] = 0x24
  362. tmp[0x2B] = 0x20
  363. tmp[0x2C] = 0x22
  364. p.actionTable = append(p.actionTable, tmp)
  365. p.gotoTable = make(map[int64]map[int64]int64)
  366. tmp = make(map[int64]int64)
  367. tmp[0xB] = 0x33
  368. tmp[0x3] = 0x1F
  369. p.gotoTable[0x18] = tmp
  370. tmp = make(map[int64]int64)
  371. tmp[0x0] = 0xF
  372. p.gotoTable[0x13] = tmp
  373. tmp = make(map[int64]int64)
  374. tmp[0x0] = 0x11
  375. tmp[0x1] = 0x15
  376. tmp[0x2] = 0x17
  377. tmp[0x14] = 0x2B
  378. tmp[0x7] = 0x2B
  379. tmp[0x9] = 0x2B
  380. tmp[0xA] = 0x2B
  381. tmp[0x1D] = 0x2B
  382. tmp[0xF] = 0x3D
  383. p.gotoTable[0x14] = tmp
  384. tmp = make(map[int64]int64)
  385. tmp[0x16] = 0x43
  386. p.gotoTable[0x15] = tmp
  387. tmp = make(map[int64]int64)
  388. tmp[0x0] = 0x13
  389. tmp[0x1] = 0x13
  390. tmp[0x2] = 0x13
  391. tmp[0x3] = 0x1D
  392. tmp[0x7] = 0x13
  393. tmp[0x20] = 0x51
  394. tmp[0x9] = 0x13
  395. tmp[0xA] = 0x13
  396. tmp[0xB] = 0x1D
  397. tmp[0xE] = 0x3B
  398. tmp[0xF] = 0x13
  399. tmp[0x14] = 0x13
  400. tmp[0x18] = 0x3B
  401. tmp[0x1D] = 0x13
  402. p.gotoTable[0x16] = tmp
  403. tmp = make(map[int64]int64)
  404. tmp[0x9] = 0x2D
  405. tmp[0xA] = 0x2D
  406. tmp[0x14] = 0x2D
  407. tmp[0x1D] = 0x2D
  408. tmp[0x7] = 0x2D
  409. p.gotoTable[0x17] = tmp
  410. p.prodList = []*productionItem{{0x19, 0x2}, {0x13, 0x1},
  411. {0x13, 0x4}, {0x15, 0x2}, {0x15, 0x0},
  412. {0x14, 0x3}, {0x14, 0x3}, {0x14, 0x2},
  413. {0x14, 0x2}, {0x14, 0x2}, {0x14, 0x2},
  414. {0x14, 0x3}, {0x14, 0x4}, {0x14, 0x2},
  415. {0x14, 0x1}, {0x17, 0x3}, {0x17, 0x4},
  416. {0x17, 0x5}, {0x17, 0x4}, {0x18, 0x4},
  417. {0x18, 0x2}, {0x18, 0x1}, {0x18, 0x3},
  418. {0x16, 0x1}, {0x16, 0x1}}
  419. p.inputTable = make(map[string]int64)
  420. p.inputTable["/"] = 0x2
  421. p.inputTable["["] = 0x9
  422. p.inputTable["escc"] = 0x12
  423. p.inputTable["]"] = 0xA
  424. p.inputTable["("] = 0x4
  425. p.inputTable["{"] = 0xC
  426. p.inputTable["^"] = 0xB
  427. p.inputTable[")"] = 0x5
  428. p.inputTable["|"] = 0x3
  429. p.inputTable["?"] = 0x8
  430. p.inputTable["}"] = 0xE
  431. p.inputTable["+"] = 0x7
  432. p.inputTable["*"] = 0x6
  433. p.inputTable[","] = 0xF
  434. p.inputTable["<$>"] = 0x1
  435. p.inputTable["-"] = 0x11
  436. p.inputTable["c"] = 0x10
  437. p.inputTable["d"] = 0xD
  438. return
  439. }
  440. func (p *Parser) put(data interface{}) error {
  441. if fmt.Sprint(data) == "dupl" {
  442. p.duplCount++
  443. if p.duplCount > 100 {
  444. return fmt.Errorf("dupl commands over 100")
  445. }
  446. }
  447. return nil
  448. }
  449. func (p *Parser) parse(lx *lexer) (ret interface{}, err error) {
  450. var (
  451. act int64
  452. token string
  453. tokenID int64
  454. state int64
  455. actObj int64
  456. stateStack = list.New()
  457. outputStack []interface{}
  458. )
  459. stateStack.PushBack(uint(0))
  460. for {
  461. if token, err = lx.getToken(); err != nil {
  462. return
  463. }
  464. tokenID = p.inputTable[token]
  465. state = int64(stateStack.Front().Value.(uint))
  466. actObj = p.actionTable[tokenID][state]
  467. if actObj == 0 {
  468. err = fmt.Errorf("Parse Error: %s", lx.getPositionInfo())
  469. return
  470. }
  471. act = actObj
  472. if act == 1 {
  473. ret = outputStack[len(outputStack)-1]
  474. return
  475. } else if (act & 1) == 1 {
  476. outputStack = append(outputStack, lx.yyText)
  477. stateStack.PushFront(uint(act)>>1 - 1)
  478. lx.advanced = true
  479. } else if (act & 1) == 0 {
  480. pi := uint(act) >> 1
  481. length := p.prodList[pi].bodyLength
  482. var result interface{}
  483. if length > 0 {
  484. result = outputStack[int64(len(outputStack))-length]
  485. }
  486. switch pi {
  487. case 0x1:
  488. result = p.codes
  489. case 0x2:
  490. result = p.codes
  491. case 0x3:
  492. p.flagsC = outputStack[len(outputStack)-1].(string)
  493. if !strings.Contains(p.flags, p.flagsC) {
  494. err = fmt.Errorf("Flag Repeated:%s", lx.getPositionInfo())
  495. return
  496. }
  497. if !strings.Contains("igm", p.flagsC) {
  498. err = fmt.Errorf("Unknow Flag:%s", lx.getPositionInfo())
  499. return
  500. }
  501. p.flags += p.flagsC
  502. case 0x4:
  503. case 0x5:
  504. if err = p.put("or"); err != nil {
  505. return
  506. }
  507. case 0x6:
  508. case 0x7:
  509. case 0x8:
  510. if err = p.put("star"); err != nil {
  511. return
  512. }
  513. case 0x9:
  514. if err = p.put("more"); err != nil {
  515. return
  516. }
  517. case 0xA:
  518. if err = p.put("ask"); err != nil {
  519. return
  520. }
  521. case 0xB:
  522. if err = p.put([]interface{}{"include", outputStack[len(outputStack)-2]}); err != nil {
  523. return
  524. }
  525. case 0xC:
  526. if err = p.put([]interface{}{"exclude", outputStack[len(outputStack)-2]}); err != nil {
  527. return
  528. }
  529. case 0xD:
  530. if err = p.put("cat"); err != nil {
  531. return
  532. }
  533. case 0xE:
  534. if err = p.put("single"); err != nil {
  535. return
  536. }
  537. case 0xF:
  538. p.num = (outputStack[len(outputStack)-2]).(int64) - 1
  539. for p.num > 0 {
  540. if err = p.put("dupl"); err != nil {
  541. return
  542. }
  543. p.num--
  544. }
  545. p.num = (outputStack[len(outputStack)-2]).(int64) - 1
  546. for p.num > 0 {
  547. if err = p.put("cat"); err != nil {
  548. return
  549. }
  550. p.num--
  551. }
  552. case 0x10:
  553. if err = p.put("ask"); err != nil {
  554. return
  555. }
  556. p.num = (outputStack[len(outputStack)-2]).(int64) - 1
  557. for p.num > 0 {
  558. if err = p.put("dupl"); err != nil {
  559. return
  560. }
  561. p.num--
  562. }
  563. p.num = (outputStack[len(outputStack)-2]).(int64) - 1
  564. for p.num > 0 {
  565. if err = p.put("cat"); err != nil {
  566. return
  567. }
  568. p.num--
  569. }
  570. case 0x11:
  571. p.num = (outputStack[len(outputStack)-4]).(int64) - 1
  572. for p.num > 0 {
  573. if err = p.put("dupl"); err != nil {
  574. return
  575. }
  576. p.num--
  577. }
  578. p.num = (outputStack[len(outputStack)-2]).(int64) - (outputStack[len(outputStack)-4]).(int64)
  579. if p.num > 0 {
  580. if err = p.put("dupl"); err != nil {
  581. return
  582. }
  583. if err = p.put("ask"); err != nil {
  584. return
  585. }
  586. for p.num > 1 {
  587. if err = p.put("dupl"); err != nil {
  588. return
  589. }
  590. p.num--
  591. }
  592. }
  593. p.num = (outputStack[len(outputStack)-2]).(int64) - 1
  594. for p.num > 0 {
  595. if err = p.put("cat"); err != nil {
  596. return
  597. }
  598. p.num--
  599. }
  600. case 0x12:
  601. p.num = (outputStack[len(outputStack)-3]).(int64)
  602. for p.num > 0 {
  603. if err = p.put("dupl"); err != nil {
  604. return
  605. }
  606. p.num--
  607. }
  608. if err = p.put("star"); err != nil {
  609. return
  610. }
  611. p.num = (outputStack[len(outputStack)-3]).(int64)
  612. for p.num > 0 {
  613. if err = p.put("cat"); err != nil {
  614. return
  615. }
  616. p.num--
  617. }
  618. case 0x13:
  619. p.num = (outputStack[len(outputStack)-4]).(int64)
  620. result = p.num + 1
  621. if err = p.put([]interface{}{"c", (outputStack[len(outputStack)-3])}); err != nil {
  622. return
  623. }
  624. if err = p.put([]interface{}{"c", (outputStack[len(outputStack)-1])}); err != nil {
  625. return
  626. }
  627. if err = p.put("range"); err != nil {
  628. return
  629. }
  630. case 0x14:
  631. p.num = (outputStack[len(outputStack)-2]).(int64)
  632. result = p.num + 1
  633. if err = p.put("single"); err != nil {
  634. return
  635. }
  636. case 0x15:
  637. if err = p.put("single"); err != nil {
  638. return
  639. }
  640. result = int64(1)
  641. case 0x16:
  642. if err = p.put([]interface{}{"c", (outputStack[len(outputStack)-3])}); err != nil {
  643. return
  644. }
  645. if err = p.put([]interface{}{"c", (outputStack[len(outputStack)-1])}); err != nil {
  646. return
  647. }
  648. if err = p.put("range"); err != nil {
  649. return
  650. }
  651. result = int64(1)
  652. case 0x17:
  653. if err = p.put([]interface{}{"c", (outputStack[len(outputStack)-1])}); err != nil {
  654. return
  655. }
  656. case 0x18:
  657. if "\\c" == outputStack[len(outputStack)-1].(string) {
  658. err = fmt.Errorf("Control Character:%s", lx.getPositionInfo())
  659. return
  660. }
  661. if err = p.put([]interface{}{"escc", (outputStack[len(outputStack)-1])}); err != nil {
  662. return
  663. }
  664. }
  665. /** actions applying end **/
  666. var j int64
  667. var e *list.Element
  668. for j < length {
  669. e = stateStack.Front()
  670. stateStack.Remove(e)
  671. outputStack = outputStack[:len(outputStack)-1]
  672. j++
  673. }
  674. state = int64(stateStack.Front().Value.(uint))
  675. actObj = p.gotoTable[p.prodList[pi].headerID][state]
  676. if actObj == 0 {
  677. err = fmt.Errorf("goto error! %s", lx.getPositionInfo())
  678. return
  679. }
  680. act = actObj
  681. stateStack.PushFront(uint(act)>>1 - 1)
  682. outputStack = append(outputStack, result)
  683. }
  684. }
  685. }
  686. // Parse parse input string.
  687. func (p *Parser) Parse(source string) (result interface{}, err error) {
  688. defer func() {
  689. if e := recover(); e != nil {
  690. err = fmt.Errorf("panic:%v", e)
  691. return
  692. }
  693. }()
  694. if source == "" {
  695. return
  696. }
  697. lx := newLexer()
  698. lx.setSource(source)
  699. return p.parse(lx)
  700. }