This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

84 řádky
2.0 KiB

  1. from . import html5
  2. from .examples import examples
  3. from lark import Lark
  4. from lark.tree import Tree
  5. class App(html5.Div):
  6. def __init__(self):
  7. super().__init__("""
  8. <h1>
  9. <img src="lark-logo.png"> IDE
  10. </h1>
  11. <main>
  12. <menu>
  13. <select [name]="examples">
  14. <option disabled selected>Examples</option>
  15. </select>
  16. <select [name]="parser">
  17. <option value="earley" selected>Earley (default)</option>
  18. <option value="lalr">LALR</option>
  19. <option value="cyk">CYK</option>
  20. </select>
  21. </menu>
  22. <div id="inputs">
  23. <div>
  24. <div>Grammar:</div>
  25. <textarea [name]="grammar" id="grammar" placeholder="Lark Grammar..."></textarea>
  26. </div>
  27. <div>
  28. <div>Input:</div>
  29. <textarea [name]="input" id="input" placeholder="Parser input..."></textarea>
  30. </div>
  31. </div>
  32. <div id="result">
  33. <ul [name]="ast" />
  34. </div>
  35. </main>
  36. """)
  37. self.sinkEvent("onKeyUp", "onChange")
  38. self.parser = "earley"
  39. # Pre-load examples
  40. for name, (grammar, input) in examples.items():
  41. option = html5.Option(name)
  42. option.grammar = grammar
  43. option.input = input
  44. self.examples.appendChild(option)
  45. def onChange(self, e):
  46. if html5.utils.doesEventHitWidgetOrChildren(e, self.examples):
  47. example = self.examples.children(self.examples["selectedIndex"])
  48. self.grammar["value"] = example.grammar.strip()
  49. self.input["value"] = example.input.strip()
  50. self.onKeyUp()
  51. elif html5.utils.doesEventHitWidgetOrChildren(e, self.parser):
  52. self.parser = self.parser.children(self.parser["selectedIndex"])["value"]
  53. self.onKeyUp()
  54. def onKeyUp(self, e=None):
  55. l = Lark(self.grammar["value"], parser=self.parser)
  56. try:
  57. ast = l.parse(self.input["value"])
  58. except Exception as e:
  59. self.ast.appendChild(
  60. html5.Li(str(e)), replace=True
  61. )
  62. print(ast)
  63. traverse = lambda node: html5.Li([node.data, html5.Ul([traverse(c) for c in node.children])] if isinstance(node, Tree) else node)
  64. self.ast.appendChild(traverse(ast), replace=True)
  65. def start():
  66. html5.Body().appendChild(
  67. App()
  68. )