efp

package module
v0.0.0-...-9ad904a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2024 License: BSD-3-Clause Imports: 3 Imported by: 75

README

EFP (Excel Formula Parser)

Build Status Code Coverage Go Report Card go.dev Licenses FOSSA Status

Using EFP (Excel Formula Parser) you can get an Abstract Syntax Tree (AST) from Excel formula.

Installation

go get github.com/xuri/efp

Example

package main

import "github.com/xuri/efp"

func main() {
    ps := efp.ExcelParser()
    ps.Parse("=SUM(A3+B9*2)/2")
    println(ps.PrettyPrint())
}

Get AST

SUM <Function> <Start>
    A3 <Operand> <Range>
    + <OperatorInfix> <Math>
    B9 <Operand> <Range>
    * <OperatorInfix> <Math>
    2 <Operand> <Number>
 <Function> <Stop>
/ <OperatorInfix> <Math>
2 <Operand> <Number>

Contributing

Contributions are welcome! Open a pull request to fix a bug, or open an issue to discuss a new feature or change.

Credits

EFP (Excel Formula Parser) is a Go language port of E. W. Bachtal's Excel formula parser.

Licenses

This program is under the terms of the BSD 3-Clause License. See https://opensource.org/licenses/BSD-3-Clause.

FOSSA Status

Documentation

Overview

Package efp (Excel Formula Parser) tokenize an Excel formula using an implementation of E. W. Bachtal's algorithm.

Go language version by Ri Xu: https://xuri.me

Index

Constants

View Source
const (
	// Character constants
	QuoteDouble  = '"'
	QuoteSingle  = '\''
	BracketClose = ']'
	BracketOpen  = '['
	BraceOpen    = '{'
	BraceClose   = '}'
	ParenOpen    = '('
	ParenClose   = ')'
	Semicolon    = ';'
	Whitespace   = ' '
	Comma        = ','
	ErrorStart   = '#'

	OperatorsSN      = "+-"
	OperatorsInfix   = "+-*/^&=><"
	OperatorsPostfix = '%'

	// Token type
	TokenTypeNoop            = "Noop"
	TokenTypeOperand         = "Operand"
	TokenTypeFunction        = "Function"
	TokenTypeSubexpression   = "Subexpression"
	TokenTypeArgument        = "Argument"
	TokenTypeOperatorPrefix  = "OperatorPrefix"
	TokenTypeOperatorInfix   = "OperatorInfix"
	TokenTypeOperatorPostfix = "OperatorPostfix"
	TokenTypeWhitespace      = "Whitespace"
	TokenTypeUnknown         = "Unknown"

	// Token subtypes
	TokenSubTypeStart         = "Start"
	TokenSubTypeStop          = "Stop"
	TokenSubTypeText          = "Text"
	TokenSubTypeNumber        = "Number"
	TokenSubTypeLogical       = "Logical"
	TokenSubTypeError         = "Error"
	TokenSubTypeRange         = "Range"
	TokenSubTypeMath          = "Math"
	TokenSubTypeConcatenation = "Concatenation"
	TokenSubTypeIntersection  = "Intersection"
	TokenSubTypeUnion         = "Union"
)

QuoteDouble, QuoteSingle and other's constants are token definitions.

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

type Parser struct {
	Formula string

	Tokens     Tokens
	TokenStack Tokens
	Offset     int
	InString   bool
	InPath     bool
	InRange    bool
	InError    bool
	// contains filtered or unexported fields
}

Parser inheritable container. TokenStack directly maps a LIFO stack of tokens.

func ExcelParser

func ExcelParser() Parser

ExcelParser provides function to parse an Excel formula into a stream of tokens.

func (*Parser) EOF

func (ps *Parser) EOF() bool

EOF provides function to check whether end of tokens stack.

func (*Parser) Parse

func (ps *Parser) Parse(formula string) []Token

Parse provides function to parse formula as a token stream (list).

func (*Parser) PrettyPrint

func (ps *Parser) PrettyPrint() string

PrettyPrint provides function to pretty the parsed result with the indented format.

func (*Parser) Render

func (ps *Parser) Render() string

Render provides function to get formatted formula after parsed.

type Token

type Token struct {
	TValue   string
	TType    string
	TSubType string
}

Token encapsulate a formula token.

type Tokens

type Tokens struct {
	Index int
	Items []Token
}

Tokens directly maps the ordered list of tokens. Attributes:

items - Ordered list
index - Current position in the list

func (*Tokens) BOF

func (tk *Tokens) BOF() bool

BOF provides function to check whether beginning of list.

func (*Tokens) EOF

func (tk *Tokens) EOF() bool

EOF provides function to check whether end of list.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL