func (p *Earley) PartialParse(tokens Tokens, chartIndex int) *earleyChart {
	debug.Debugf("------- starting partial parse %v\n", chartIndex)
	p.words = append(p.words, tokens...)
	p.resizeChartIfNecessary(p.words)
	p.chart.setInputWords(p.words)
	currStateSet := p.chart.GetState(chartIndex)
	for _, token := range tokens {
		setIndex := 0
		for {
			if setIndex >= len(currStateSet.GetStates()) {
				break
			}
			item := currStateSet.items[setIndex]
			fromItems := []ItemId{item.id}
			if !item.isCompleted() {
				if !item.GetRightSymbolByRulePos().isTerminal() {
					p.predict(item, chartIndex, fromItems)
				} else {
					p.scan(item, chartIndex, fromItems, token)
				}
			} else {  
				p.complete(item, chartIndex)
			}
			setIndex++
		}
		chartIndex++
		currStateSet = p.chart.GetState(chartIndex)
		debug.Debugf("------\n%v\n------\n", p.chart.String())
	}
	return p.chart
}
This is the incremental bit of our parser. We can basically feed in
a list of words (i.e. lexed tokens) and parse at a specific word index.
package earley
import (
	"sigs.k8s.io/instrumentation-tools/debug"
)
const Cursor = "\u25EC"
type Earley struct {
	g     Grammar
	chart *earleyChart
	words Tokens
}
func NewEarleyParser(g Grammar) *Earley {
	newChart := initializeChart(g)
	return &Earley{g: g, chart: newChart}
}
func (p *Earley) predict(state *EarleyItem, chartIndex int, fromItems []ItemId) {
	nextSymbol := state.GetRightSymbolByRulePos().(nonTerminal)
	recognizedRules := p.g.recognizedRules(nextSymbol)
	currStateSet := p.chart.GetState(chartIndex)
	if len(recognizedRules) > 0 {
		debug.Debugf("Predicting next state\n")
	}
	for _, r := range recognizedRules {
		ctx := Copy(state.ctx)
		nextItem := newPredictItem(r, chartIndex, fromItems, ctx)
		if currStateSet.Add(nextItem) {
			debug.Debugf("added %v\n", nextItem.String())
		}
	}
}
func (p *Earley) scan(state *EarleyItem, chartIndex int, fromItems []ItemId, token Tokhan) {
	if chartIndex+1 >= p.chart.Length() || !state.DoesTokenTypeMatch(token) {
		return
	}
	debug.Debugf("Token (%v) matches, performing scan \n", token)
	ctx := &completionContext{}
	if state.ctx != nil {
		ctx = Copy(state.ctx)
	}
	ctx.BuildContext(state.GetRightSymbolTypeByRulePos(), &token)
	nextItem := newScanItem(state, state.originatingIndex, fromItems, ctx)
	debug.Debugf("Scanning next item : %v\n", nextItem)
	nextSet := p.chart.GetState(chartIndex + 1)
	nextSet.Add(nextItem)
}
func (p *Earley) complete(state *EarleyItem, chartIndex int) {
	currStateSet := p.chart.GetState(chartIndex)
	originalSet := p.chart.GetState(state.originatingIndex)
	itemsToComplete := originalSet.findItemsToComplete(state.Rule.left)
	for _, item := range itemsToComplete {
		fromItems := []ItemId{state.id, item.id}
		ctx := Copy(state.ctx)
		nextItem := newCompleteItem(&item, fromItems, ctx)
		if currStateSet.Add(nextItem) {
			debug.Debugf("completed %v\n", nextItem.String())
		}
	}
}
func (p *Earley) resizeChartIfNecessary(words Tokens) {
	p.resizeChart(len(words) + 1)
}
func (p *Earley) resizeChart(size int) {
	currentSize := p.chart.Length()
	for currentSize < size {
		p.chart.append(NewStateSet())
		currentSize += 1
	}
}
func (p *Earley) Parse(input string) *earleyChart {
	inputWords := extractWords(input)
	return p.ParseTokens(inputWords)
}
func (p *Earley) ParseTokens(tokens Tokens) *earleyChart {
	p.chart.resetChartBeforeIndex(0)
	p.PartialParse(tokens, 0)
	debug.Debugf("------\n%v\n------\n", p.chart.String())
	return p.chart
}