parse.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. package attl
  2. // ParseFunc is a parser function.
  3. // It parses the input input starting from *index, which must be
  4. // guaranteed by the caller to be non-nil.
  5. // It should return as follows:
  6. // * If the parse function matched what it is intended to parse
  7. // it should return the parsed value, nil, and index should be moved to
  8. // point right after the parsed part of te string.
  9. // * If the parse function did not match what it is intended to parse
  10. // it should retirn nil, nil, and index should be unchanged.
  11. // * If the parse function did match what it is intended to parse
  12. // but there is a parse error, it should return nil, *Error,
  13. // and index should be set to the error location.
  14. type ParseFunc func(input []rune, index *int) (Value, *Error)
  15. var Debug = false
  16. func debug(msg string) {
  17. if Debug {
  18. print(msg)
  19. }
  20. }
  21. func ParseAlternative(input []rune, index *int, funcs ...ParseFunc) (Value, *Error) {
  22. for _, fun := range funcs {
  23. val, err := fun(input, index)
  24. if err != nil || val != nil {
  25. return val, err
  26. }
  27. }
  28. return nil, nil
  29. }
  30. func ParseWhileRuneOk(input []rune, index *int, ok func(r rune) bool) (Value, *Error) {
  31. length := len(input)
  32. start, now := *index, 0
  33. for ; *index < length; *index++ {
  34. r := input[*index]
  35. if !ok(r) {
  36. if now == 0 {
  37. return nil, nil
  38. }
  39. return String(input[start:*index]), nil
  40. }
  41. now++
  42. }
  43. return nil, ErrorFromString("unexpected EOF: >" + string(input[start:*index]) + "<")
  44. }
  45. type LineInfo struct {
  46. Line int
  47. From int
  48. To int
  49. }
  50. type LineIndex []LineInfo
  51. func PhysicalLineIndex(input []rune) LineIndex {
  52. res := LineIndex{}
  53. line, last, index := 0, 0, 0
  54. for ; index < len(input); index++ {
  55. ch := input[index]
  56. if ch == '\n' {
  57. line++
  58. li := LineInfo{line, last, index}
  59. last = index
  60. res = append(res, li)
  61. }
  62. }
  63. li := LineInfo{line, last, index}
  64. res = append(res, li)
  65. return res
  66. }
  67. func (li LineIndex) Lookup(index int) (row, col int) {
  68. for _, info := range li {
  69. if index >= info.From && index < info.To {
  70. return info.Line, index - info.From
  71. }
  72. }
  73. return -1, -1
  74. }
  75. func Parse(input string) (value Value, rerr *Error) {
  76. index := 0
  77. return ParseScript([]rune(input), &index)
  78. }
  79. func ParseScript(input []rune, index *int) (value Value, rerr *Error) {
  80. defer func() {
  81. val := recover()
  82. err, ok := val.(*Error)
  83. if ok {
  84. rerr.Children = append(rerr.Children, err)
  85. }
  86. }()
  87. value, rerr = ParseStatements([]rune(input), index)
  88. if value != nil {
  89. value = Block{value.(List)}
  90. }
  91. return value, rerr
  92. }
  93. func IsEof(input []rune, index *int) bool {
  94. return *index >= len(input)
  95. }
  96. func ParseStatements(input []rune, index *int) (Value, *Error) {
  97. debug("ParseStatements")
  98. statements := List{}
  99. for {
  100. val, err := ParseStatement(input, index)
  101. if err != nil {
  102. debug("error in statement")
  103. return nil, err
  104. }
  105. if val != nil {
  106. statements = append(statements, val)
  107. }
  108. sep, err := ParseRs(input, index)
  109. if IsEof(input, index) {
  110. return statements, nil
  111. }
  112. if err != nil {
  113. debug("error in rs")
  114. return nil, err
  115. }
  116. if sep == nil {
  117. return statements, nil
  118. }
  119. }
  120. }
  121. func ParseRs(input []rune, index *int) (Value, *Error) {
  122. debug("ParseRs")
  123. SkipWs(input, index)
  124. return ParseWhileRuneOk(input, index, func(r rune) bool {
  125. return r == '\n' || r == '\r' || r == ';'
  126. })
  127. }
  128. func ParseWs(input []rune, index *int) (Value, *Error) {
  129. debug("ParseWs")
  130. return ParseWhileRuneOk(input, index, func(r rune) bool {
  131. return r == ' ' || r == '\t'
  132. })
  133. }
  134. func ParseWsRs(input []rune, index *int) (Value, *Error) {
  135. debug("ParseRs")
  136. SkipWs(input, index)
  137. return ParseWhileRuneOk(input, index, func(r rune) bool {
  138. return r == '\n' || r == '\r' || r == ';' || r == ' ' || r == '\t'
  139. })
  140. }
  141. func SkipWs(input []rune, index *int) {
  142. ParseWs(input, index)
  143. }
  144. func SkipRs(input []rune, index *int) {
  145. ParseRs(input, index)
  146. }
  147. func SkipWsRs(input []rune, index *int) {
  148. ParseWsRs(input, index)
  149. }
  150. func ParseComment(input []rune, index *int) (Value, *Error) {
  151. debug("ParseComment")
  152. start := *index
  153. if !RequireRune(input, index, '#') {
  154. return nil, nil
  155. }
  156. for ; *index < len(input); *index++ {
  157. r := input[*index]
  158. if r == '\n' || r == '\r' {
  159. end := *index
  160. return Comment(string(input[start:end])), nil
  161. }
  162. }
  163. return nil, ErrorFromString("unexpected EOF in comment")
  164. }
  165. func ParseStatement(input []rune, index *int) (Value, *Error) {
  166. debug("ParseStatement")
  167. SkipWs(input, index)
  168. return ParseAlternative(input, index, ParseCommand, ParseBlock, ParseComment)
  169. }
  170. func ParseParameters(input []rune, index *int) (Value, *Error) {
  171. debug("ParseParameters")
  172. params := List{}
  173. for {
  174. sep, err := ParseWs(input, index)
  175. if err != nil {
  176. return nil, err
  177. }
  178. if sep == nil {
  179. return params, nil
  180. }
  181. val, err := ParseParameter(input, index)
  182. if err != nil {
  183. return nil, err
  184. }
  185. if val == nil {
  186. return params, nil
  187. }
  188. params = append(params, val)
  189. }
  190. }
  191. func ParseParameter(input []rune, index *int) (Value, *Error) {
  192. debug("ParseParameter")
  193. funcs := []ParseFunc{ParseLiteral, ParseEvaluation, ParseBlock, ParseGetter}
  194. return ParseAlternative(input, index, funcs...)
  195. }
  196. func ParseOrder(input []rune, index *int) (Value, *Error) {
  197. debug("ParseOrder")
  198. return ParseAlternative(input, index, ParseLiteral, ParseEvaluation)
  199. }
  200. func ParseCommand(input []rune, index *int) (Value, *Error) {
  201. debug("ParseCommand")
  202. order, err := ParseOrder(input, index)
  203. if err != nil || order == nil {
  204. return order, err
  205. }
  206. params, err := ParseParameters(input, index)
  207. if err != nil {
  208. return params, err
  209. }
  210. if params == nil {
  211. params = List{}
  212. }
  213. return Command{order, params.(List)}, nil
  214. }
  215. // RequireRune requires a single rune to be present,
  216. // and skips it, however that rune is discared.
  217. // Returns true if the rune was found, false if not
  218. func RequireRune(input []rune, index *int, req rune) bool {
  219. if input[*index] == req {
  220. *index++
  221. return true
  222. }
  223. return false
  224. }
  225. func ParseEvaluation(input []rune, index *int) (Value, *Error) {
  226. debug("ParseEvaluation")
  227. if !RequireRune(input, index, '[') {
  228. return nil, nil
  229. }
  230. res, err := ParseCommand(input, index)
  231. if err != nil {
  232. return nil, err
  233. }
  234. if !RequireRune(input, index, ']') {
  235. print(input[*index])
  236. return nil, ErrorFromString("Expected end of evaluation ]")
  237. }
  238. if res != nil {
  239. res = Evaluation{Command: res.(Command)}
  240. }
  241. return res, nil
  242. }
  243. func ParseBlock(input []rune, index *int) (Value, *Error) {
  244. debug("ParseBlock")
  245. if !RequireRune(input, index, '{') {
  246. return nil, nil
  247. }
  248. res, err := ParseStatements(input, index)
  249. if err != nil {
  250. return nil, err
  251. }
  252. SkipWsRs(input, index)
  253. if !RequireRune(input, index, '}') {
  254. return nil, ErrorFromString("Expected end of block }")
  255. }
  256. return Block{Statements: res.(List)}, nil
  257. return nil, nil
  258. }
  259. func ParseGetter(input []rune, index *int) (Value, *Error) {
  260. debug("ParseGetter")
  261. if RequireRune(input, index, '$') {
  262. if input[*index] == '$' { // recusively parse double getters
  263. val, err := ParseGetter(input, index)
  264. if err == nil { // Getter with a getter inside.
  265. return Getter{val}, err
  266. } else {
  267. return nil, err
  268. }
  269. } else { // integer, sring or getter name
  270. key, err := ParseLiteral(input, index)
  271. if key == nil {
  272. return nil, ErrorFromString("Expected literal after getter $")
  273. }
  274. if err == nil {
  275. return Getter{key}, nil
  276. }
  277. return nil, err
  278. }
  279. }
  280. return nil, nil
  281. }
  282. func ParseLiteral(input []rune, index *int) (Value, *Error) {
  283. debug("ParseLiteral")
  284. return ParseAlternative(input, index, ParseWord, ParseString, ParseInteger,
  285. ParseRawString)
  286. }
  287. func IsLetter(r rune) bool {
  288. return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r > rune(128)) ||
  289. r == '_' || r == '/'
  290. }
  291. func IsNumber(r rune) bool {
  292. return (r >= '0' && r <= '9')
  293. }
  294. func ParseWord(input []rune, index *int) (Value, *Error) {
  295. debug("ParseWord")
  296. // a word consists of an ascii letter or non asci characters, or underscore
  297. // followed by an ascii letter or number, or non ascii characters, or underscore
  298. start := *index
  299. r := input[*index]
  300. if !IsLetter(r) {
  301. return nil, nil
  302. }
  303. for *index++; *index < len(input); *index++ {
  304. r := input[*index]
  305. if !(IsLetter(r) || IsNumber(r)) {
  306. return Word(string(input[start:*index])), nil
  307. }
  308. }
  309. return nil, ErrorFromString("unexpected EOF in string")
  310. }
  311. func next(input []rune, index *int) {
  312. *index++
  313. if *index >= len(input) {
  314. panic(ErrorFromString("Unexpected end of input."))
  315. }
  316. }
  317. func ParseEscape(input []rune, index *int) (Value, *Error) {
  318. res := ""
  319. if input[*index] != '\\' {
  320. return nil, nil
  321. }
  322. next(input, index)
  323. switch input[*index] {
  324. case 'a':
  325. res += "\a"
  326. case 'b':
  327. res += "\b"
  328. case 'e':
  329. res += "\033"
  330. case 'f':
  331. res += "\f"
  332. case 'n':
  333. res += "\n"
  334. case 'r':
  335. res += "\r"
  336. case 't':
  337. res += "\t"
  338. case '\\':
  339. res += "\\"
  340. case '"':
  341. res += "\""
  342. default:
  343. return nil, ErrorFromString("Unknown escape sequence character")
  344. }
  345. return String(res), nil
  346. }
  347. func ParseString(input []rune, index *int) (Value, *Error) {
  348. debug("ParseString")
  349. res := ""
  350. ch := input[*index]
  351. if ch != '"' {
  352. return nil, nil
  353. }
  354. *index++
  355. for *index < len(input) {
  356. ch = input[*index]
  357. esc, err := ParseEscape(input, index)
  358. if err != nil {
  359. return nil, err
  360. }
  361. if esc != nil {
  362. res += string(esc.(String))
  363. } else if ch == '"' {
  364. *index++
  365. return String(res), nil
  366. } else {
  367. res += string(ch)
  368. }
  369. *index++
  370. }
  371. return nil, ErrorFromString("Unexpected end of input.")
  372. }
  373. func ParseRawString(input []rune, index *int) (Value, *Error) {
  374. debug("ParseRawString")
  375. res := ""
  376. ch := input[*index]
  377. if ch != '`' {
  378. return nil, nil
  379. }
  380. *index++
  381. for *index < len(input) {
  382. ch = input[*index]
  383. if ch == '`' {
  384. *index++
  385. return String(res), nil
  386. } else {
  387. res += string(ch)
  388. }
  389. *index++
  390. }
  391. return nil, ErrorFromString("Unexpected end of input.")
  392. }
  393. func ParseInteger(input []rune, index *int) (Value, *Error) {
  394. debug("ParseInteger")
  395. ch := input[*index]
  396. neg := 1
  397. res := 0
  398. if ch == '-' {
  399. neg = -1
  400. } else if ch == '+' {
  401. // do nothing, ignore + as an integer prefix
  402. } else {
  403. res = int(ch - '0')
  404. if res < 0 || res > 9 { // Not a digit, no integer
  405. return nil, nil
  406. }
  407. }
  408. *index++
  409. for *index < len(input) {
  410. ch = input[*index]
  411. ch -= '0'
  412. if ch < 0 || ch > 9 { // Not a digit, finished
  413. return Int(neg * res), nil
  414. }
  415. res = res * 10
  416. res = res + int(ch)
  417. *index++
  418. }
  419. return nil, ErrorFromString("unexpected EOF in number")
  420. }