Guest User

Untitled

a guest
May 22nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. {- TEST 1
  2.  
  3. Hi Evan, there is some kind of weird intereaction going on
  4. inside of
  5.  
  6. ```
  7. latexExpression : Parser LatexExpression
  8. latexExpression =
  9. oneOf
  10. [ lazy (\_ -> environment) -- (1)
  11. , inlineMath ws -- (2)
  12. , words -- (3)
  13. ]
  14. ```
  15.  
  16. If 1,2,3 are active, the commands
  17.  
  18. > run environment env1
  19. > run environtment env2
  20.  
  21. where env1 = "\\begin{th}$a^2 = b^3$\\end{th}"
  22. and env2 = "\\begin{th}\nIt's all gonna be OK!\n\\end{th}"
  23.  
  24. blow the stack. But the commands
  25.  
  26. > run latexExpression "$a^2 = b^3$"
  27. > run latexExpression "one two three"
  28.  
  29.  
  30. succeed. If 1,2 are active, then `run environment env1`
  31. command works -- weird.
  32.  
  33. To the extent that I've been able to fix problems,
  34. they have all had to do with the diffrence between\
  35. chomping _up to_ a string versus chomping _up through_
  36. a string. Here I don't see what is going on.
  37. -}
  38.  
  39.  
  40. module Test2 exposing (..)
  41.  
  42. import Parser exposing (..)
  43.  
  44.  
  45. latexExpression : Parser LatexExpression
  46. latexExpression =
  47. oneOf
  48. [ lazy (\_ -> environment)
  49. , inlineMath ws
  50. , words
  51. ]
  52.  
  53.  
  54. env1 =
  55. "\\begin{th}$a^2 = b^3$\\end{th}"
  56.  
  57.  
  58. env2 =
  59. "\\begin{th}\nIt's all gonna be OK!\n\\end{th}"
  60.  
  61.  
  62.  
  63. {- Data type for AST -}
  64.  
  65.  
  66. type LatexExpression
  67. = LXString String
  68. | InlineMath String
  69. | Environment String (List LatexExpression) LatexExpression -- Environment name optArgs body
  70. | LatexList (List LatexExpression)
  71. | LXError String
  72.  
  73.  
  74.  
  75. {- MATH -}
  76.  
  77.  
  78. inlineMath : Parser () -> Parser LatexExpression
  79. inlineMath wsParser =
  80. succeed InlineMath
  81. |. symbol "$"
  82. |= parseTo "$"
  83. |. wsParser
  84.  
  85.  
  86.  
  87. {- WORDS -}
  88.  
  89.  
  90. word : Parser String
  91. word =
  92. getChompedString <|
  93. succeed ()
  94. |. chompIf (\c -> Char.isAlphaNum c)
  95. |. chompWhile notSpecialCharacter
  96.  
  97.  
  98. notSpecialCharacter : Char -> Bool
  99. notSpecialCharacter c =
  100. not (c == ' ' || c == '\n' || c == '\\' || c == '$')
  101.  
  102.  
  103. words : Parser LatexExpression
  104. words =
  105. itemListWithSeparator ws word
  106. |> map (String.join " ")
  107. |> map LXString
  108.  
  109.  
  110.  
  111. {- ENVIRONMENT -}
  112.  
  113.  
  114. environment : Parser LatexExpression
  115. environment =
  116. lazy (\_ -> envName |> andThen environmentOfType)
  117.  
  118.  
  119. envName : Parser String
  120. envName =
  121. (succeed identity
  122. |. spaces
  123. |. symbol "\\begin{"
  124. |= parseTo "}"
  125. )
  126.  
  127.  
  128. environmentOfType : String -> Parser LatexExpression
  129. environmentOfType envType =
  130. let
  131. theEndWord =
  132. "\\end{" ++ envType ++ "}"
  133.  
  134. envKind =
  135. envType
  136. in
  137. environmentParser theEndWord envType
  138.  
  139.  
  140. environmentParser : String -> String -> Parser LatexExpression
  141. environmentParser endWord_ envType =
  142. (succeed identity
  143. |. ws
  144. |= itemList latexExpression
  145. |. ws
  146. |. symbol endWord_
  147. |. ws
  148. |> map LatexList
  149. |> map (Environment envType [])
  150. )
  151.  
  152.  
  153.  
  154. {- PARSER HELPERS -}
  155.  
  156.  
  157. parseTo : String -> Parser String
  158. parseTo marker =
  159. (getChompedString <|
  160. succeed identity
  161. |= chompUntilEndOr marker
  162. |. symbol marker
  163. )
  164. |> map (String.dropRight (String.length marker))
  165.  
  166.  
  167.  
  168. {- ITEM LIST -}
  169.  
  170.  
  171. itemList : Parser a -> Parser (List a)
  172. itemList itemParser =
  173. itemList_ [] itemParser
  174.  
  175.  
  176. itemList_ : List a -> Parser a -> Parser (List a)
  177. itemList_ initialList itemParser =
  178. Parser.loop initialList (itemListHelper itemParser)
  179.  
  180.  
  181. itemListHelper : Parser a -> List a -> Parser (Step (List a) (List a))
  182. itemListHelper itemParser revItems =
  183. oneOf
  184. [ succeed (\item -> Loop (item :: revItems))
  185. |= itemParser
  186. , succeed ()
  187. |> Parser.map (\_ -> Done (List.reverse revItems))
  188. ]
  189.  
  190.  
  191. itemListWithSeparator : Parser () -> Parser a -> Parser (List a)
  192. itemListWithSeparator separatorParser itemParser =
  193. Parser.loop [] (itemListWithSeparatorHelper separatorParser itemParser)
  194.  
  195.  
  196. itemListWithSeparatorHelper : Parser () -> Parser a -> List a -> Parser (Step (List a) (List a))
  197. itemListWithSeparatorHelper separatorParser itemParser revItems =
  198. oneOf
  199. [ succeed (\w -> Loop (w :: revItems))
  200. |= itemParser
  201. |. separatorParser
  202. , succeed ()
  203. |> Parser.map (\_ -> Done (List.reverse revItems))
  204. ]
  205.  
  206.  
  207.  
  208. {- Char -> Bool -}
  209.  
  210.  
  211. space : Parser ()
  212. space =
  213. chompWhile (\c -> c == ' ')
  214.  
  215.  
  216. ws : Parser ()
  217. ws =
  218. chompWhile (\c -> c == ' ' || c == '\n')
Add Comment
Please, Sign In to add comment