test-lr-script-expression.scm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. ;;; test-lr-script-expression.scm --
  2. ;;
  3. ;;Parse scripts, each line an expression.
  4. ;;
  5. (load "common-test.scm")
  6. (define (doit . tokens)
  7. (let ((parser (lalr-parser (expect: 0)
  8. (N O C T (left: A) (left: M) (nonassoc: U))
  9. (script (lines) : (reverse $1))
  10. (lines (lines line) : (cons $2 $1)
  11. (line) : (list $1))
  12. (line (T) : #\newline
  13. (E T) : $1
  14. (error T) : (list 'error-clause $2))
  15. (E (N) : $1
  16. (E A E) : ($2 $1 $3)
  17. (E M E) : ($2 $1 $3)
  18. (A E (prec: U)) : ($1 $2)
  19. (O E C) : $2))))
  20. (parser (make-lexer tokens) error-handler)))
  21. ;;; --------------------------------------------------------------------
  22. ;;; Correct input
  23. (check
  24. (doit (make-lexical-token 'T #f #\newline))
  25. => '(#\newline))
  26. (check
  27. (doit (make-lexical-token 'N #f 1)
  28. (make-lexical-token 'T #f #\newline))
  29. => '(1))
  30. (check
  31. (doit (make-lexical-token 'N #f 1)
  32. (make-lexical-token 'A #f +)
  33. (make-lexical-token 'N #f 2)
  34. (make-lexical-token 'T #f #\newline))
  35. => '(3))
  36. (check
  37. (doit (make-lexical-token 'N #f 1)
  38. (make-lexical-token 'A #f +)
  39. (make-lexical-token 'N #f 2)
  40. (make-lexical-token 'M #f *)
  41. (make-lexical-token 'N #f 3)
  42. (make-lexical-token 'T #f #\newline))
  43. => '(7))
  44. (check
  45. (doit (make-lexical-token 'O #f #\()
  46. (make-lexical-token 'N #f 1)
  47. (make-lexical-token 'A #f +)
  48. (make-lexical-token 'N #f 2)
  49. (make-lexical-token 'C #f #\))
  50. (make-lexical-token 'M #f *)
  51. (make-lexical-token 'N #f 3)
  52. (make-lexical-token 'T #f #\newline))
  53. => '(9))
  54. (check
  55. (doit (make-lexical-token 'O #f #\()
  56. (make-lexical-token 'N #f 1)
  57. (make-lexical-token 'A #f +)
  58. (make-lexical-token 'N #f 2)
  59. (make-lexical-token 'C #f #\))
  60. (make-lexical-token 'M #f *)
  61. (make-lexical-token 'N #f 3)
  62. (make-lexical-token 'T #f #\newline)
  63. (make-lexical-token 'N #f 4)
  64. (make-lexical-token 'M #f /)
  65. (make-lexical-token 'N #f 5)
  66. (make-lexical-token 'T #f #\newline))
  67. => '(9 4/5))
  68. ;;; --------------------------------------------------------------------
  69. (check
  70. ;;Successful error recovery.
  71. (doit (make-lexical-token 'O #f #\()
  72. (make-lexical-token 'N #f 1)
  73. (make-lexical-token 'A #f +)
  74. (make-lexical-token 'N #f 2)
  75. (make-lexical-token 'M #f *)
  76. (make-lexical-token 'N #f 3)
  77. (make-lexical-token 'T #f #\newline)
  78. (make-lexical-token 'N #f 4)
  79. (make-lexical-token 'M #f /)
  80. (make-lexical-token 'N #f 5)
  81. (make-lexical-token 'T #f #\newline))
  82. => '((error-clause #f)
  83. 4/5))
  84. (check
  85. ;;Unexpected end of input.
  86. (let ((r (doit (make-lexical-token 'N #f 1)
  87. (make-lexical-token 'A #f +)
  88. (make-lexical-token 'N #f 2))))
  89. (cons r *error*))
  90. => '(#f (error-handler "Syntax error: unexpected end of input")))
  91. (check
  92. ;;Unexpected end of input.
  93. (let ((r (doit (make-lexical-token 'N #f 1)
  94. (make-lexical-token 'A #f +)
  95. (make-lexical-token 'T #f #\newline))))
  96. (cons r *error*))
  97. => '(((error-clause #f))
  98. (error-handler "Syntax error: unexpected token : " . T)))
  99. ;;; end of file