parser.mly 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. %{
  2. open Tex
  3. open Render_info
  4. let sq_close_ri = HTMLABLEC(FONT_UFH,"]", "]")
  5. %}
  6. %token <Render_info.t> LITERAL DELIMITER
  7. %token <string> FUN_AR2 FUN_INFIX FUN_AR1 DECL FUN_AR1opt BIG FUN_AR2nb
  8. %token <string*string> BOX
  9. %token <string*(string*string)> FUN_AR1hl
  10. %token <string*Render_info.font_force> FUN_AR1hf DECLh
  11. %token <string*(Tex.t->Tex.t->string*string*string)> FUN_AR2h
  12. %token <string*(Tex.t list->Tex.t list->string*string*string)> FUN_INFIXh
  13. %token EOF CURLY_OPEN CURLY_CLOSE SUB SUP SQ_CLOSE NEXT_CELL NEXT_ROW
  14. %token BEGIN__MATRIX BEGIN_PMATRIX BEGIN_BMATRIX BEGIN_BBMATRIX BEGIN_VMATRIX BEGIN_VVMATRIX BEGIN_CASES BEGIN_ARRAY BEGIN_ALIGN BEGIN_ALIGNAT BEGIN_SMALLMATRIX
  15. %token END__MATRIX END_PMATRIX END_BMATRIX END_BBMATRIX END_VMATRIX END_VVMATRIX END_CASES END_ARRAY END_ALIGN END_ALIGNAT END_SMALLMATRIX
  16. %token LEFT RIGHT
  17. %type <Tex.t list> tex_expr
  18. %start tex_expr
  19. %%
  20. tex_expr:
  21. expr EOF { $1 }
  22. | ne_expr FUN_INFIX ne_expr EOF
  23. { [TEX_INFIX($2,$1,$3)] }
  24. | ne_expr FUN_INFIXh ne_expr EOF
  25. { let t,h=$2 in [TEX_INFIXh(t,h,$1,$3)] }
  26. expr:
  27. /* */ { [] }
  28. | ne_expr { $1 }
  29. ne_expr:
  30. lit_aq expr { $1 :: $2 }
  31. | litsq_aq expr { $1 :: $2 }
  32. | DECLh expr { let t,h = $1 in [TEX_DECLh(t,h,$2)] }
  33. litsq_aq:
  34. litsq_zq { $1 }
  35. | litsq_dq { let base,downi = $1 in TEX_DQ(base,downi) }
  36. | litsq_uq { let base,upi = $1 in TEX_UQ(base,upi)}
  37. | litsq_fq { $1 }
  38. litsq_fq:
  39. litsq_dq SUP lit { let base,downi = $1 in TEX_FQ(base,downi,$3) }
  40. | litsq_uq SUB lit { let base,upi = $1 in TEX_FQ(base,$3,upi) }
  41. litsq_uq:
  42. litsq_zq SUP lit { $1,$3 }
  43. litsq_dq:
  44. litsq_zq SUB lit { $1,$3 }
  45. litsq_zq:
  46. | SQ_CLOSE { TEX_LITERAL sq_close_ri }
  47. expr_nosqc:
  48. /* */ { [] }
  49. | lit_aq expr_nosqc { $1 :: $2 }
  50. lit_aq:
  51. lit { $1 }
  52. | lit_dq { let base,downi = $1 in TEX_DQ(base,downi) }
  53. | lit_uq { let base,upi = $1 in TEX_UQ(base,upi)}
  54. | lit_dqn { TEX_DQN($1) }
  55. | lit_uqn { TEX_UQN($1) }
  56. | lit_fq { $1 }
  57. lit_fq:
  58. lit_dq SUP lit { let base,downi = $1 in TEX_FQ(base,downi,$3) }
  59. | lit_uq SUB lit { let base,upi = $1 in TEX_FQ(base,$3,upi) }
  60. | lit_dqn SUP lit { TEX_FQN($1, $3) }
  61. lit_uq:
  62. lit SUP lit { $1,$3 }
  63. lit_dq:
  64. lit SUB lit { $1,$3 }
  65. lit_uqn:
  66. SUP lit { $2 }
  67. lit_dqn:
  68. SUB lit { $2 }
  69. left:
  70. LEFT DELIMITER { $2 }
  71. | LEFT SQ_CLOSE { sq_close_ri }
  72. right:
  73. RIGHT DELIMITER { $2 }
  74. | RIGHT SQ_CLOSE { sq_close_ri }
  75. lit:
  76. LITERAL { TEX_LITERAL $1 }
  77. | DELIMITER { TEX_LITERAL $1 }
  78. | BIG DELIMITER { TEX_BIG ($1,$2) }
  79. | BIG SQ_CLOSE { TEX_BIG ($1,sq_close_ri) }
  80. | left expr right { TEX_LR ($1,$3,$2) }
  81. | FUN_AR1 lit { TEX_FUN1($1,$2) }
  82. | FUN_AR1hl lit { let t,h=$1 in TEX_FUN1hl(t,h,$2) }
  83. | FUN_AR1hf lit { let t,h=$1 in TEX_FUN1hf(t,h,$2) }
  84. | FUN_AR1opt expr_nosqc SQ_CLOSE lit { TEX_FUN2sq($1,TEX_CURLY $2,$4) }
  85. | FUN_AR2 lit lit { TEX_FUN2($1,$2,$3) }
  86. | FUN_AR2nb lit lit { TEX_FUN2nb($1,$2,$3) }
  87. | FUN_AR2h lit lit { let t,h=$1 in TEX_FUN2h(t,h,$2,$3) }
  88. | BOX { let bt,s = $1 in TEX_BOX (bt,s) }
  89. | CURLY_OPEN expr CURLY_CLOSE
  90. { TEX_CURLY $2 }
  91. | CURLY_OPEN ne_expr FUN_INFIX ne_expr CURLY_CLOSE
  92. { TEX_INFIX($3,$2,$4) }
  93. | CURLY_OPEN ne_expr FUN_INFIXh ne_expr CURLY_CLOSE
  94. { let t,h=$3 in TEX_INFIXh(t,h,$2,$4) }
  95. | BEGIN__MATRIX matrix END__MATRIX { TEX_MATRIX ("matrix", $2) }
  96. | BEGIN_PMATRIX matrix END_PMATRIX { TEX_MATRIX ("pmatrix", $2) }
  97. | BEGIN_BMATRIX matrix END_BMATRIX { TEX_MATRIX ("bmatrix", $2) }
  98. | BEGIN_BBMATRIX matrix END_BBMATRIX { TEX_MATRIX ("Bmatrix", $2) }
  99. | BEGIN_VMATRIX matrix END_VMATRIX { TEX_MATRIX ("vmatrix", $2) }
  100. | BEGIN_VVMATRIX matrix END_VVMATRIX { TEX_MATRIX ("Vmatrix", $2) }
  101. | BEGIN_ARRAY matrix END_ARRAY { TEX_MATRIX ("array", $2) }
  102. | BEGIN_ALIGN matrix END_ALIGN { TEX_MATRIX ("aligned", $2) }
  103. | BEGIN_ALIGNAT matrix END_ALIGNAT { TEX_MATRIX ("alignedat", $2) }
  104. | BEGIN_SMALLMATRIX matrix END_SMALLMATRIX { TEX_MATRIX ("smallmatrix", $2) }
  105. | BEGIN_CASES matrix END_CASES { TEX_MATRIX ("cases", $2) }
  106. matrix:
  107. line { [$1] }
  108. | line NEXT_ROW matrix { $1::$3 }
  109. line:
  110. expr { [$1] }
  111. | expr NEXT_CELL line { $1::$3 }
  112. ;;