example.k 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. \l parse.k
  2. para:`0:
  3. para"Make a state machine that parses quoted strings handline escapes"
  4. para"----------------------------------------------------------------"
  5. para"First identify the relevant characters and give the classes names."
  6. :G:("\"\\";"QS") / quotes and slashes
  7. para"Define a table indicating relevant transitions."
  8. para"The first row and column are labels and the rest are transitions."
  9. para"By convention the first class is . representing \"everything else\"."
  10. :Q:(" .QS"
  11. "..Q."
  12. "QQ.E"
  13. "EQQQ")
  14. para"\nParse table to generate the classification table and transition matrix."
  15. :(C;TT):sm[G;Q]
  16. para"\nSome text:"
  17. `0:txt:"This is \"text\" with \"quotes\" and an escaped escape \"here\\\\\" but not here: \\\\"
  18. para"\nParse text using config and start state."
  19. s:1_*+Q
  20. c:1_*Q
  21. P:prs[(C;TT);s?"."]
  22. s@P@txt
  23. para""
  24. para"With the convention that the first class is \"everything else\", you can combine state tables mechanically."
  25. :G:("\"/\\\n ";"QSENW";"..Q...W") / quotes, slashes, newlines and spaces
  26. para"Comments start at slash and end at newlines, but must be preceded by whitespace."
  27. :C:(" .SNW"
  28. "....W"
  29. "W.C.W"
  30. "CCC.C")
  31. para"To aggregate, need the order of the classes in the aggregate table"
  32. para"and which states to transition to from the generic class ."
  33. para"Each sub-state machine declares which classes it handles explicitly."
  34. para"(i.e. which classes don't default to handling as a generic class.)"
  35. :T:mrg[-2#G;((Q;,"Q");(C;""))]
  36. s:1_*+T
  37. c:".",G[1]
  38. para"\nMake a comment:"
  39. `0:txt:"This is \"text\" with \"quotes\" and an / escaped escape \"here\\\\\" but not here: \\\\"
  40. P:prs[sm[2#G;T];s?"."]
  41. para"\nParse"
  42. `0:pp:s@P@txt
  43. `0:txt
  44. para""
  45. `0:,/":",/:+(pp;txt)
  46. para"\nChallenge: Change state machine to include the closing quote."