1"use strict";(self.webpackChunkexpr_lang_org=self.webpackChunkexpr_lang_org||[]).push([[5986],{1171:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>c,contentTitle:()=>a,default:()=>p,frontMatter:()=>s,metadata:()=>r,toc:()=>l});const r=JSON.parse('{"id":"visitor","title":"Visitor","description":"Expr provides an interface to traverse the AST of the expression before the compilation.","source":"@site/versioned_docs/version-v1.16/visitor.md","sourceDirName":".","slug":"/visitor","permalink":"/docs/v1.16/visitor","draft":false,"unlisted":false,"editUrl":"https://github.com/expr-lang/expr/tree/master/docs/visitor.md","tags":[],"version":"v1.16","frontMatter":{},"sidebar":"docs","previous":{"title":"Functions","permalink":"/docs/v1.16/functions"},"next":{"title":"Patch","permalink":"/docs/v1.16/patch"}}');var i=t(4848),o=t(8453);const s={},a="Visitor",c={},l=[];function d(e){const n={a:"a",admonition:"admonition",code:"code",h1:"h1",header:"header",p:"p",pre:"pre",...(0,o.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(n.header,{children:(0,i.jsx)(n.h1,{id:"visitor",children:"Visitor"})}),"\n",(0,i.jsxs)(n.p,{children:["Expr provides an interface to traverse the ",(0,i.jsx)("span",{title:"Abstract Syntax Tree",style:{borderBottom:"1px dotted currentColor"},children:"AST"})," of the expression before the compilation.\nThe ",(0,i.jsx)(n.code,{children:"Visitor"})," interface allows you to collect information about the expression, modify the expression, or even generate\na new expression."]}),"\n",(0,i.jsxs)(n.p,{children:["Let's start with an ",(0,i.jsx)(n.a,{href:"https://pkg.go.dev/github.com/expr-lang/expr/ast#Visitor",children:"ast.Visitor"})," implementation which will\ncollect all variables used in the expression."]}),"\n",(0,i.jsxs)(n.p,{children:["Visitor must implement a single method ",(0,i.jsx)(n.code,{children:"Visit(*ast.Node)"}),", which will be called for each node in the AST."]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-go",children:"type Visitor struct {\n Identifiers []string\n}\n\nfunc (v *Visitor) Visit(node *ast.Node) {\n if n, ok := (*node).(*ast.IdentifierNode); ok {\n v.Identifiers = append(v.Identifiers, n.Value)\n }\n}\n"})}),"\n",(0,i.jsxs)(n.p,{children:["Full list of available AST nodes can be found in the ",(0,i.jsx)(n.a,{href:"https://pkg.go.dev/github.com/expr-lang/expr/ast",children:"ast"})," documentation."]}),"\n",(0,i.jsxs)(n.p,{children:["Let's parse the expression and use ",(0,i.jsx)(n.a,{href:"https://pkg.go.dev/github.com/expr-lang/expr/ast#Walk",children:"ast.Walk"})," to traverse the AST:"]}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-go",children:"tree, err := parser.Parse(`foo + bar`)\nif err != nil {\n panic(err)\n}\n\nv := &Visitor{}\n// highlight-next-line\nast.Walk(&tree.Node, v)\n\nfmt.Println(v.Identifiers) // [foo, bar]\n"})}),"\n",(0,i.jsxs)(n.admonition,{type:"note",children:[(0,i.jsx)(n.p,{children:"Although it is possible to access the AST of compiled program, it may be already be modified by patchers, optimizers, etc."}),(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-go",children:"program, err := expr.Compile(`foo + bar`)\nif err != nil {\n panic(err)\n}\n\n// highlight-next-line\nnode := program.Node()\n\nv := &Visitor{}\nast.Walk(&node, v)\n"})})]})]})}function p(e={}){const{wrapper:n}={...(0,o.R)(),...e.components};return n?(0,i.jsx)(n,{...e,children:(0,i.jsx)(d,{...e})}):d(e)}},8453:(e,n,t)=>{t.d(n,{R:()=>s,x:()=>a});var r=t(6540);const i={},o=r.createContext(i);function s(e){const n=r.useContext(o);return r.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function a(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:s(e.components),r.createElement(o.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.