Skip to content

Latest commit

 

History

History
82 lines (64 loc) · 2.77 KB

parser-examples.org

File metadata and controls

82 lines (64 loc) · 2.77 KB

Implementation

Open this document in Emacs and type C-c C-c to execute the code examples!

(require 'reazon)
(require 'tree-edit)
(require 'tree-edit-java-grammar)

(setq-local java-grammar
            (with-mode-local java-mode tree-edit-grammar))

A simple example

(reazon-run 10 (q)
  (reazon-set-equalo q '(1 2 3 4)))

Relational parser (try statement)

(reazon-run 5 (tokens)
  ;; TOKENS is a list of tokens that parses as a 'try_statement'. (Ignore the '())
  (tree-edit-parseo (alist-get 'try_statement java-grammar) tokens '()))

Relational parser (function arguments)

(reazon-run 5 (tokens)
  ;; TOKENS is a list of tokens that parses as a 'argument_list'.
  (tree-edit-parseo (alist-get 'argument_list java-grammar) tokens '()))

Inserting new tokens into a node (1)

Insert an expression after x in func(x)

(reazon-run 1 (new-tokens)
  (reazon-fresh (tokens)
    ;; A token of type 'expression' exists in the new tokens
    (reazon-membero 'expression new-tokens)
    ;; NEW-TOKENS is preceeded by "(" and is followed by ")", where TOKENS is the entire list.
    (tree-edit--prefixpostfixo '("(" expression) new-tokens '(")") tokens)
    ;; TOKENS is a list of tokens that parses as a 'argument_list'.
    (tree-edit-parseo (alist-get 'argument_list java-grammar) tokens '())))

Inserting new tokens into a node (2)

Insert an expression before x in func(x)

(reazon-run 1 (new-tokens)
  (reazon-fresh (tokens)
    ;; A token of type 'expression' exists in the new tokens
    (reazon-membero 'expression new-tokens)
    ;; NEW-TOKENS is preceeded by "(" and is followed by ")", where TOKENS is the entire list.
    (tree-edit--prefixpostfixo '("(") new-tokens '(expression ")") tokens)
    ;; TOKENS is a list of tokens that parses as a 'argument_list'.
    (tree-edit-parseo (alist-get 'argument_list java-grammar) tokens '())))

Inserting new tokens into a node (3)

Insert another expression after if (...) {...}

(reazon-run 1 (new-tokens)
  (reazon-fresh (tokens)
    ;; TOKENS is a list of tokens that parses as a 'argument_list'.
    (tree-edit-parseo (alist-get 'if_statement java-grammar) tokens '())
    ;; NEW-TOKENS is preceeded by "(" and is followed by ")", where TOKENS is the entire list.
    (tree-edit--prefixpostfixo '("if" parenthesized_expression statement) new-tokens '() tokens)
    ;; A token of type 'expression' exists in the new tokens
    (reazon-membero 'statement new-tokens)))