Proposal: Improve ast.Walk and ast.Visitor
Mantenedores costumam responder em até 1 dia
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Facilidade para iniciantes
- 35/100
Direção de pesquisa
Comece lendo as implementações atuais de ast.Walk e ast.Visitor e, em seguida, acompanhe como checker lida com visitors, incluindo os métodos ocultos existentes ShouldRepeat e Reset. O trabalho estará concluído quando um design acordado e compatível abordar a interrupção dos walks, os erros dos visitors, o estado do resultado e a reutilização de checker sem depender de acoplamento oculto.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
Problem
There are a few minor extensibility issues with the current implementation:
- A visitor cannot stop
ast.Walk: even if it finished its work it will still run for the reminder of the tree. If e.g. I want to make a silly visitor to answer the question "was this identifier used?" then it will keep running. It would be useful to be able to tellast.Walkto stop earlier to be more efficient. - A visitor cannot return an error: a common problem when customizing a language is that you may want to walk an AST and fail on some custom logic. For example, if you want regular expressions to be compiled and cached when the whole expression is compiled for performance, then you can make a visitor that returns an error if the
matchesoperator was used and its RHS is not a constant string. In this case, it would be useful that a visitor can tell that. - There is coupled, scattered and hidden code about visitors: for instance, the operator overloading patcher visitor implements additional methods (
ShouldRepeat() boolandReset()) that are checked in private code inchecker, socheckeris still coupled withpatcherby the means of knowing this hidden methods. - A visitor could make no changes but the AST is still re-checked: writing custom visitors can be more expensive than needed since each run will trigger the checker. For simple visitors that only want to check something instead of changing the tree, this can be excessive. It would be interesting to be able to avoid re-checking when not needed.
These items can make the language more difficult to maintain on the long run because if more features are needed, then more hidden methods are added, then more internal coupling, etc.
Proposed solution
There is no need to break the current API, many users may already rely on their visitors running correctly, and this proposal is rather light on outwards changes. With these changes, expr will be more extendable and new features will be easier to implement. Also, users will not need to hook into hidden logic. This will encourage users to implement their own logic instead of further expanding the current base language, which can make it more easily maintainable on the long run.
Changes to provide the ability to stop ast.Walk
package ast
type Visitor interface { // same as today
Visit(node *Node)
}
type StoppableVisitor interface {
Visit(node *Node) (stop bool)
}
type StoppableVisitorFunc func(node *Node) (stop bool) // trivial adapter
func (f StoppableVisitorFunc) Visit(node *Node) (stop bool) {
return f(node)
}
func StoppableVisitorFromVisitor(v Visitor) StoppableVisitor {
return StoppableVisitorFunc(func(node *Node) (stop bool) {
v.Visit(node)
return false // never stop, always keep going as it is today
})
}
func Walk(node *Node, v Visitor) {
vv, _ := v.(StoppableVisitor)
if vv == nil {
vv = StoppableVisitorFromVisitor(v)
}
StoppableWalk(vv)
}
func StoppableWalk(node *Node, v StoppableVisitor) {
// this func doesn't need to return anything so just
// wrap the actual recursive one
walk(node, v)
}
func walk(node *Node, v StoppableVisitor) (stop bool) {
// move the logic of the current Walk here, but check if the
// bool was true then exit instead of continuing
}
Changes to provide additional state
The following changes would allow checker
package ast
type ResultsVisitor interface {
StoppableVisitor
// Results provides additional details about the outcome
// that can be used in other packages of expr. We don't
// need to add e.g. a "Reset" method because reset logic
// could also be implemented in this same method, so
// footprint is small.
Results() (VisitorResults, error)
}
type VisitorResults struct {
// ChangedState determines if the tree was changed, in
// which case the checker will be ran afterwards.
ChangedState bool
// ShouldRepeat determines if the visitor should be run
// again, similar to the current "ShouldRepeat" method
ShouldRepeat bool
// no other things for now, but being a struct makes it
// easy to further expand it for new features without
// requiring big changes
}
package checker
// Checker is now reusable.
type Checker struct {
Config *conf.Config
// internal stuff
}
func (c *Checker) CheckAndRunVisitors(tree *parser.Tree) error {
// For every visitor run, see if it implements ResultsVisitor
// to see if ChangedState requires re-checking of the tree.
// Also see if ShouldRepeat is true, then do the appropriate.
// Also, if it returns an error, then end and return it.
// If the visitor does not implement ResultsVisitor, then assume
// that we don't need to repeat BUT assume that state was changed
// as ChangedState is just a particular optimization for the case
// that we didn't change state.
}
- Linguagem predominante
- Go
- Estrelas
- 8.1k
- Forks
- 544
- Merge médio
- 1d 9h
- PRs com merge (30d)
- 25
Preparar o ambiente
Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de expr-lang/expr
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 90/100
expr-lang/expr#1037 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
count() without a predicate compiles for non-bool arrays and fails at run timeTalvez já em andamento @minilan677-netizen assumiu há 2 dias. Aberta
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 25/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 57/100
Mantenedores costumam responder em até 1 dia
-
OSS-Fuzz issue 578739164Abertawontfix
Dificuldade 4/5 1-2 dias Facilidade para iniciantes 35/100
expr-lang/expr#995 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
-
`find`, `findIndex`, `first` and `get` are type-checked as the element type where they return nilTalvez livre de novo Um pull request para esta issue foi fechado sem ser mesclado. Aberta
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 65/100
expr-lang/expr#989 · 1 comentário ·
Mantenedores costumam responder em até 1 dia
Todas as issues de expr-lang/expr
Issues semelhantes
-
factory:subtask
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 95/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 95/100
OneBusAway/watchdog#470 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 92/100
Mantenedores costumam responder em até 1 dia
-
area: session
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
charmbracelet/crush#4484 ·
Mantenedores costumam responder em até 1 dia
-
accepted good first issue
Dificuldade 1/5 1-3 horas Facilidade para iniciantes 95/100