diff options
| author | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-20 15:31:05 +0100 | 
|---|---|---|
| committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-04-20 15:31:05 +0100 | 
| commit | 9bffe238bd97d1dc606c84f2dfc4c73c99b36eea (patch) | |
| tree | da8501c35300d7b09db986db6f933e4fda6be454 /subex | |
| parent | 79ac135cee306cbcfa4b73f1b7c0b404712fee7b (diff) | |
| download | stred-go-9bffe238bd97d1dc606c84f2dfc4c73c99b36eea.tar | |
Properly exports all SubexASTs
Diffstat (limited to 'subex')
| -rw-r--r-- | subex/parse.go | 14 | ||||
| -rw-r--r-- | subex/subexast.go | 104 | 
2 files changed, 59 insertions, 59 deletions
| diff --git a/subex/parse.go b/subex/parse.go index 4b1aa6e..5675ae0 100644 --- a/subex/parse.go +++ b/subex/parse.go @@ -305,7 +305,7 @@ func parseSubex(l RuneReader, minPower int) SubexAST {  			}  			lhs = SubexASTOutput {replacement}  		default: -			lhs = SubexASTCopyAtom{atom: walk.StringAtom(r)} +			lhs = SubexASTCopyAtom{Atom: walk.StringAtom(r)}  	}  	loop: for {  		if minPower <= 0 { @@ -319,8 +319,8 @@ func parseSubex(l RuneReader, minPower int) SubexAST {  		switch {  			case r == '{' && minPower <= 8:  				lhs = SubexASTRepeat { -					content: lhs, -					acceptable: parseRepeatRange(l), +					Content: lhs, +					Acceptable: parseRepeatRange(l),  				}  			case r == '+' && minPower <= 8:  				lhs = SubexASTSum {lhs} @@ -338,8 +338,8 @@ func parseSubex(l RuneReader, minPower int) SubexAST {  					panic("Missing slot character")  				}  				lhs = SubexASTStore{ -					match: lhs, -					slot: slot, +					Match: lhs, +					Slot: slot,  				}  			case r == '|' && minPower <= 4:  				rhs := parseSubex(l, 5) @@ -353,8 +353,8 @@ func parseSubex(l RuneReader, minPower int) SubexAST {  					panic("Missing subex after ;")  				}  				lhs = SubexASTJoin{ -					content: lhs, -					delimiter: rhs, +					Content: lhs, +					Delimiter: rhs,  				}  			default:  				l.Rewind() diff --git a/subex/subexast.go b/subex/subexast.go index bc80835..87686b1 100644 --- a/subex/subexast.go +++ b/subex/subexast.go @@ -12,64 +12,64 @@ type SubexAST interface {  // Process the first subex, then the second, splitting the input text in two  type SubexASTConcat struct { -	first, second SubexAST +	First, Second SubexAST  }  func (ast SubexASTConcat) compileWith(next SubexState) SubexState { -	return ast.first.compileWith(ast.second.compileWith(next)) +	return ast.First.compileWith(ast.Second.compileWith(next))  }  func (ast SubexASTConcat) String() string { -	return fmt.Sprintf("(%v)(%v)", ast.first, ast.second) +	return fmt.Sprintf("(%v)(%v)", ast.First, ast.Second)  }  // Processing a subex and storing the output in a slot instead of outputting it  type SubexASTStore struct { -	match SubexAST -	slot rune +	Match SubexAST +	Slot rune  }  func (ast SubexASTStore) compileWith(next SubexState) SubexState {  	return &SubexCaptureBeginState { -		next: ast.match.compileWith(&SubexStoreEndState { -			slot: ast.slot, +		next: ast.Match.compileWith(&SubexStoreEndState { +			slot: ast.Slot,  			next: next,  		}),  	}  }  func (ast SubexASTStore) String() string { -	return fmt.Sprintf("$%c(%v)", ast.slot, ast.match) +	return fmt.Sprintf("$%c(%v)", ast.Slot, ast.Match)  }  // Try to run the first subex, if it fails then backtrack and use the second  type SubexASTOr struct { -	first, second SubexAST +	First, Second SubexAST  }  func (ast SubexASTOr) compileWith(next SubexState) SubexState {  	return &SubexGroupState { -		ast.first.compileWith(next), -		ast.second.compileWith(next), +		ast.First.compileWith(next), +		ast.Second.compileWith(next),  	}  }  type ConvexRange struct { -	start, end int +	Start, End int  }  func (cr ConvexRange) minmax() (int, int) { -		if cr.start == -1 { -			return cr.end, -1 -		} else if cr.end == -1 { -			return cr.start, -1 -		} else if cr.start < cr.end { -			return cr.start, cr.end +		if cr.Start == -1 { +			return cr.End, -1 +		} else if cr.End == -1 { +			return cr.Start, -1 +		} else if cr.Start < cr.End { +			return cr.Start, cr.End  		} else { -			return cr.end, cr.start +			return cr.End, cr.Start  		}  }  func (cr ConvexRange) decrement() ConvexRange { -	if cr.start == -1 { -		return ConvexRange{-1, cr.end - 1} -	} else if cr.end == -1 { -		return ConvexRange{cr.start - 1, -1} +	if cr.Start == -1 { +		return ConvexRange{-1, cr.End - 1} +	} else if cr.End == -1 { +		return ConvexRange{cr.Start - 1, -1}  	} else { -		return ConvexRange{cr.start - 1, cr.end - 1} +		return ConvexRange{cr.Start - 1, cr.End - 1}  	}  }  func (cr ConvexRange) compile(content SubexAST, next SubexState) SubexState { @@ -77,20 +77,20 @@ func (cr ConvexRange) compile(content SubexAST, next SubexState) SubexState {  	if min != 0 {  		return content.compileWith(cr.decrement().compile(content, next))  	} -	if cr.start == -1 { +	if cr.Start == -1 {  		state := &SubexGroupState {nil, next}  		state.first = content.compileWith(state)  		return state  	} -	if cr.end == -1 { +	if cr.End == -1 {  		state := &SubexGroupState {next, nil}  		state.second = content.compileWith(state)  		return state  	} -	if cr.end == 0 { +	if cr.End == 0 {  		state := next; -		for i := 0; i < cr.start; i += 1 { +		for i := 0; i < cr.Start; i += 1 {  			state = &SubexGroupState {  				content.compileWith(state),  				next, @@ -99,7 +99,7 @@ func (cr ConvexRange) compile(content SubexAST, next SubexState) SubexState {  		return state  	} else {  		state := next; -		for i := 0; i < cr.end; i += 1 { +		for i := 0; i < cr.End; i += 1 {  			state = &SubexGroupState {  				next,  				content.compileWith(state), @@ -112,24 +112,24 @@ func (cr ConvexRange) compile(content SubexAST, next SubexState) SubexState {  // Try to run the subex a number of times that is one of the numbers in the acceptable range  // Prioritising the left  type SubexASTRepeat struct { -	content SubexAST -	acceptable []ConvexRange +	Content SubexAST +	Acceptable []ConvexRange  }  func (ast SubexASTRepeat) compileWith(next SubexState) SubexState {  	var state SubexState = &SubexDeadState{} -	for _, convex := range ast.acceptable { -		state = &SubexGroupState {state, convex.compile(ast.content, next)} +	for _, convex := range ast.Acceptable { +		state = &SubexGroupState {state, convex.compile(ast.Content, next)}  	}  	return state  }  // Read in a single specific Atom and output it unchanged  type SubexASTCopyAtom struct { -	atom walk.Atom +	Atom walk.Atom  }  func (ast SubexASTCopyAtom) compileWith(next SubexState) SubexState {  	return &SubexCopyAtomState{ -		atom: ast.atom, +		atom: ast.Atom,  		next: next,  	}  } @@ -145,26 +145,26 @@ func (ast SubexASTCopyAny) String() string {  // Output a series of Atoms without reading anything from input  type SubexASTOutput struct { -	replacement []OutputContent +	Replacement []OutputContent  }  func (ast SubexASTOutput) compileWith(next SubexState) SubexState {  	return &SubexOutputState{ -		content: ast.replacement, +		content: ast.Replacement,  		next: next,  	}  }  // Read in a repeated subex separated by a delimiter. Greedy  type SubexASTJoin struct { -	content, delimiter SubexAST +	Content, Delimiter SubexAST  }  func (ast SubexASTJoin) compileWith(next SubexState) SubexState {  	afterContentState := &SubexGroupState {  		nil,  		next,  	} -	manyContentsState := ast.content.compileWith(afterContentState) -	afterContentState.first = ast.delimiter.compileWith(manyContentsState) +	manyContentsState := ast.Content.compileWith(afterContentState) +	afterContentState.first = ast.Delimiter.compileWith(manyContentsState)  	return &SubexGroupState {  		manyContentsState,  		next, @@ -174,11 +174,11 @@ func (ast SubexASTJoin) compileWith(next SubexState) SubexState {  // Run each input Atom through a map to produce an output Atom  // Atoms not in the map cause this to not match  type SubexASTRange struct { -	parts map[walk.Atom]walk.Atom +	Parts map[walk.Atom]walk.Atom  }  func (ast SubexASTRange) compileWith(next SubexState) SubexState {  	return &SubexRangeState { -		parts: ast.parts, +		parts: ast.Parts,  		next: next,  	}  } @@ -186,11 +186,11 @@ func (ast SubexASTRange) compileWith(next SubexState) SubexState {  // Run content, if content is a list of booleans, OR them, if all values are castable to numbers, sum them and output the total  // Reject if neither of these cases match  type SubexASTSum struct { -	content SubexAST +	Content SubexAST  }  func (ast SubexASTSum) compileWith(next SubexState) SubexState {  	return &SubexCaptureBeginState { -		next: ast.content.compileWith(&SubexArithmeticEndState { +		next: ast.Content.compileWith(&SubexArithmeticEndState {  			next: next,  			calculate: sumValues,  		}), @@ -199,11 +199,11 @@ func (ast SubexASTSum) compileWith(next SubexState) SubexState {  // Like sum but for AND and product  type SubexASTProduct struct { -	content SubexAST +	Content SubexAST  }  func (ast SubexASTProduct) compileWith(next SubexState) SubexState {  	return &SubexCaptureBeginState { -		next: ast.content.compileWith(&SubexArithmeticEndState { +		next: ast.Content.compileWith(&SubexArithmeticEndState {  			next: next,  			calculate: multiplyValues,  		}), @@ -213,11 +213,11 @@ func (ast SubexASTProduct) compileWith(next SubexState) SubexState {  // Runs the content Subex, if all outputted atoms can be cast to numbers, outputs them all negated  // Rejects if this fails  type SubexASTNegate struct { -	content SubexAST +	Content SubexAST  }  func (ast SubexASTNegate) compileWith(next SubexState) SubexState {  	return &SubexCaptureBeginState { -		next: ast.content.compileWith(&SubexArithmeticEndState { +		next: ast.Content.compileWith(&SubexArithmeticEndState {  			next: next,  			calculate: negateValues,  		}), @@ -228,11 +228,11 @@ func (ast SubexASTNegate) compileWith(next SubexState) SubexState {  // If it is a list of atoms castable to numbers, it takes the reciprocal of them all and outputs them  // Else it rejects  type SubexASTReciprocal struct { -	content SubexAST +	Content SubexAST  }  func (ast SubexASTReciprocal) compileWith(next SubexState) SubexState {  	return &SubexCaptureBeginState { -		next: ast.content.compileWith(&SubexArithmeticEndState { +		next: ast.Content.compileWith(&SubexArithmeticEndState {  			next: next,  			calculate: reciprocalValues,  		}), @@ -243,11 +243,11 @@ func (ast SubexASTReciprocal) compileWith(next SubexState) SubexState {  // Maps over the values in the output, casting each to a boolean, notting each and then outputs them  // Rejects if it cannot cast to boolean  type SubexASTNot struct { -	content SubexAST +	Content SubexAST  }  func (ast SubexASTNot) compileWith(next SubexState) SubexState {  	return &SubexCaptureBeginState { -		next: ast.content.compileWith(&SubexArithmeticEndState { +		next: ast.Content.compileWith(&SubexArithmeticEndState {  			next: next,  			calculate: notValues,  		}), | 
