nfp

package module
v0.0.0-...-ab9948c Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: BSD-3-Clause Imports: 1 Imported by: 45

README

NFP (Number Format Parser)

Build Status Code Coverage Go Report Card go.dev Licenses

Using NFP (Number Format Parser) you can get an Abstract Syntax Tree (AST) from Excel number format expression.

Installation

go get github.com/xuri/nfp

Example

package main

import "github.com/xuri/nfp"

func main() {
    ps := nfp.NumberFormatParser()
    tokens := ps.Parse("_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(@_)")
    println(p.PrettyPrint())
}

Get AST

<Positive>
      <RepeatsChar>
    # <HashPlaceHolder>
    <ThousandsSeparator>
    ## <HashPlaceHolder>
    0 <ZeroPlaceHolder>
    . <DecimalPoint>
    00 <ZeroPlaceHolder>
<Negative>
      <RepeatsChar>
    ( <Literal>
    # <HashPlaceHolder>
    , <ThousandsSeparator>
    ## <HashPlaceHolder>
    0 <ZeroPlaceHolder>
    . <DecimalPoint>
    00 <ZeroPlaceHolder>
    ) <Literal>
<Zero>
      <RepeatsChar>
    - <Literal>
    ?? <DigitalPlaceHolder>
<Text>
    @ <TextPlaceHolder>

Contributing

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

Licenses

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

Documentation

Index

Constants

View Source
const (
	// Character constants
	Asterisk       = "*"
	At             = "@"
	BackSlash      = "\\"
	BlockDelimiter = ";"
	BracketClose   = "]"
	BracketOpen    = "["
	Colon          = ":"
	Comma          = ","
	Dash           = "-"
	Dollar         = "$"
	Dot            = "."
	Hash           = "#"
	ParenClose     = ")"
	ParenOpen      = "("
	Percent        = "%"
	Plus           = "+"
	Question       = "?"
	QuoteDouble    = "\""
	QuoteSingle    = "'"
	Slash          = "/"
	Underscore     = "_"
	Whitespace     = " "
	Zero           = "0"
	// DatesTimesCodeChars defined dates and times control codes in upper case
	DatesTimesCodeChars = "AEYMDHSG"
	// NumCodeChars defined numeric code character
	NumCodeChars = "0123456789"
	// Token section types
	TokenSectionNegative = "Negative"
	TokenSectionPositive = "Positive"
	TokenSectionText     = "Text"
	TokenSectionZero     = "Zero"
	// Token subtypes
	TokenSubTypeCurrencyString = "CurrencyString"
	TokenSubTypeLanguageInfo   = "LanguageInfo"
	TokenTypeColor             = "Color"
	// Token types
	TokenTypeAlignment          = "Alignment"
	TokenTypeCondition          = "Condition"
	TokenTypeCurrencyLanguage   = "CurrencyLanguage"
	TokenTypeDateTimes          = "DateTimes"
	TokenTypeDecimalPoint       = "DecimalPoint"
	TokenTypeDenominator        = "Denominator"
	TokenTypeDigitalPlaceHolder = "DigitalPlaceHolder"
	TokenTypeElapsedDateTimes   = "ElapsedDateTimes"
	TokenTypeExponential        = "Exponential"
	TokenTypeFraction           = "Fraction"
	TokenTypeGeneral            = "General"
	TokenTypeHashPlaceHolder    = "HashPlaceHolder"
	TokenTypeLiteral            = "Literal"
	TokenTypeOperand            = "Operand"
	TokenTypeOperator           = "Operator"
	TokenTypePercent            = "Percent"
	TokenTypeRepeatsChar        = "RepeatsChar"
	TokenTypeSwitchArgument     = "SwitchArgument"
	TokenTypeTextPlaceHolder    = "TextPlaceHolder"
	TokenTypeThousandsSeparator = "ThousandsSeparator"
	TokenTypeUnknown            = "Unknown"
	TokenTypeZeroPlaceHolder    = "ZeroPlaceHolder"
)

Asterisk, At and other's constants are token definitions.

Variables

View Source
var AmPm = []string{"AM/PM", "A/P", "上午/下午"}

AmPm defined the AM and PM with international considerations.

View Source
var ColorNames = []string{
	"black",
	"blue",
	"cyan",
	"green",
	"magenta",
	"red",
	"white",
	"yellow",
}

ColorNames defined colors name used in for a section of the format, use the name of one of the following eight colors in square brackets in the section. The color code shall be the first item in the section.

View Source
var ConditionOperators = []string{"<", "<=", ">", ">=", "<>", "="}

ConditionOperators defined the condition operators.

View Source
var GeneralFormattingSwitchArguments = []string{
	"AIUEO",
	"ALPHABETIC",
	"alphabetic",
	"Arabic",
	"ARABICABJAD",
	"ARABICALPHA",
	"ArabicDash",
	"BAHTTEXT",
	"CardText",
	"CHINESENUM1",
	"CHINESENUM2",
	"CHINESENUM3",
	"CHOSUNG",
	"CIRCLENUM",
	"DBCHAR",
	"DBNUM1",
	"DBNUM2",
	"DBNUM3",
	"DBNUM4",
	"DollarText",
	"GANADA",
	"GB1",
	"GB2",
	"GB3",
	"GB4",
	"HEBREW1",
	"HEBREW2",
	"Hex",
	"HINDIARABIC",
	"HINDICARDTEXT",
	"HINDILETTER1",
	"HINDILETTER2",
	"IROHA",
	"KANJINUM1",
	"KANJINUM2",
	"KANJINUM3",
	"Ordinal",
	"OrdText",
	"Roman",
	"roman",
	"SBCHAR",
	"THAIARABIC",
	"THAICARDTEXT",
	"THAILETTER",
	"VIETCARDTEXT",
	"ZODIAC1",
	"ZODIAC2",
	"ZODIAC3",
}

GeneralFormattingSwitchArguments defined switch-arguments apply to fields whose field result is a numeric value. If the result type of the field is not numeric, then these switches have no effect.

Functions

This section is empty.

Types

type Parser

type Parser struct {
	InBracket     bool
	InString      bool
	InPlaceholder bool
	NumFmt        string
	// Runes is a copy of the number format string as a rune slice. It's stored here to avoid
	// allocating a new slice every time we need to access it.
	Runes  []rune
	Offset int
	Tokens Tokens
	Token  Token
}

Parser inheritable container.

func NumberFormatParser

func NumberFormatParser() Parser

NumberFormatParser provides function to parse an Excel number format 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(numFmt string) []Section

Parse provides function to parse number format 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.

type Part

type Part struct {
	Token Token
	Value string
}

Part directly maps the sub part of the token.

type Section

type Section struct {
	Type  string
	Items []Token
}

Section directly maps sections of the number format. Up to four sections of format codes can be specified. The format codes, separated by semicolons, define the formats for positive numbers, negative numbers, zero values, and text, in that order. If only two sections are specified, the first is used for positive numbers and zeros, and the second is used for negative numbers. If only one section is specified, it is used for all numbers. To skip a section, the ending semicolon for that section shall be written.

type Token

type Token struct {
	TValue string
	TType  string
	Parts  []Part
}

Token encapsulate a number format token.

type Tokens

type Tokens struct {
	Index        int
	SectionIndex int
	Sections     []Section
}

Tokens directly maps the ordered list of tokens. Attributes:

Index        - Current position in the number format expression
SectionIndex - Current position in section
Sections     - Ordered section of token sequences

Jump to

Keyboard shortcuts

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