some improvments
This commit is contained in:
@@ -28,9 +28,9 @@ func NewPSParser(content []byte) *PSParser {
|
||||
}
|
||||
|
||||
// Parse the postscript and store as a program that can be executed.
|
||||
func (this *PSParser) Parse() (*PSProgram, error) {
|
||||
this.skipSpaces()
|
||||
bb, err := this.reader.Peek(2)
|
||||
func (pP *PSParser) Parse() (*PSProgram, error) {
|
||||
pP.skipSpaces()
|
||||
bb, err := pP.reader.Peek(2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -38,7 +38,7 @@ func (this *PSParser) Parse() (*PSProgram, error) {
|
||||
return nil, fmt.Errorf("invalid PS Program not starting with {")
|
||||
}
|
||||
|
||||
program, err := this.parseFunction()
|
||||
program, err := pP.parseFunction()
|
||||
if err != nil && err != io.EOF {
|
||||
return nil, err
|
||||
}
|
||||
@@ -48,8 +48,8 @@ func (this *PSParser) Parse() (*PSProgram, error) {
|
||||
|
||||
// Detect the signature at the current parse position and parse
|
||||
// the corresponding object.
|
||||
func (this *PSParser) parseFunction() (*PSProgram, error) {
|
||||
c, _ := this.reader.ReadByte()
|
||||
func (pP *PSParser) parseFunction() (*PSProgram, error) {
|
||||
c, _ := pP.reader.ReadByte()
|
||||
if c != '{' {
|
||||
return nil, errors.New("invalid function")
|
||||
}
|
||||
@@ -57,8 +57,8 @@ func (this *PSParser) parseFunction() (*PSProgram, error) {
|
||||
function := NewPSProgram()
|
||||
|
||||
for {
|
||||
this.skipSpaces()
|
||||
bb, err := this.reader.Peek(2)
|
||||
pP.skipSpaces()
|
||||
bb, err := pP.reader.Peek(2)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
@@ -70,18 +70,18 @@ func (this *PSParser) parseFunction() (*PSProgram, error) {
|
||||
// Determine type.
|
||||
if bb[0] == '}' {
|
||||
common.Log.Trace("EOF function")
|
||||
this.reader.ReadByte()
|
||||
pP.reader.ReadByte()
|
||||
break
|
||||
} else if bb[0] == '{' {
|
||||
common.Log.Trace("Function!")
|
||||
inlineF, err := this.parseFunction()
|
||||
inlineF, err := pP.parseFunction()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
function.Append(inlineF)
|
||||
} else if pdfcore.IsDecimalDigit(bb[0]) || (bb[0] == '-' && pdfcore.IsDecimalDigit(bb[1])) {
|
||||
common.Log.Trace("->Number!")
|
||||
number, err := this.parseNumber()
|
||||
number, err := pP.parseNumber()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -89,24 +89,24 @@ func (this *PSParser) parseFunction() (*PSProgram, error) {
|
||||
} else {
|
||||
common.Log.Trace("->Operand or bool?")
|
||||
// Let's peek farther to find out.
|
||||
bb, _ = this.reader.Peek(5)
|
||||
bb, _ = pP.reader.Peek(5)
|
||||
peekStr := string(bb)
|
||||
common.Log.Trace("Peek str: %s", peekStr)
|
||||
|
||||
if (len(peekStr) > 4) && (peekStr[:5] == "false") {
|
||||
b, err := this.parseBool()
|
||||
b, err := pP.parseBool()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
function.Append(b)
|
||||
} else if (len(peekStr) > 3) && (peekStr[:4] == "true") {
|
||||
b, err := this.parseBool()
|
||||
b, err := pP.parseBool()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
function.Append(b)
|
||||
} else {
|
||||
operand, err := this.parseOperand()
|
||||
operand, err := pP.parseOperand()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -120,15 +120,15 @@ func (this *PSParser) parseFunction() (*PSProgram, error) {
|
||||
|
||||
// Skip over any spaces. Returns the number of spaces skipped and
|
||||
// an error if any.
|
||||
func (this *PSParser) skipSpaces() (int, error) {
|
||||
func (pP *PSParser) skipSpaces() (int, error) {
|
||||
cnt := 0
|
||||
for {
|
||||
bb, err := this.reader.Peek(1)
|
||||
bb, err := pP.reader.Peek(1)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if pdfcore.IsWhiteSpace(bb[0]) {
|
||||
this.reader.ReadByte()
|
||||
pP.reader.ReadByte()
|
||||
cnt++
|
||||
} else {
|
||||
break
|
||||
@@ -140,13 +140,13 @@ func (this *PSParser) skipSpaces() (int, error) {
|
||||
|
||||
// Numeric objects.
|
||||
// Integer or Real numbers.
|
||||
func (this *PSParser) parseNumber() (PSObject, error) {
|
||||
func (pP *PSParser) parseNumber() (PSObject, error) {
|
||||
isFloat := false
|
||||
allowSigns := true
|
||||
numStr := ""
|
||||
for {
|
||||
common.Log.Trace("Parsing number \"%s\"", numStr)
|
||||
bb, err := this.reader.Peek(1)
|
||||
bb, err := pP.reader.Peek(1)
|
||||
if err == io.EOF {
|
||||
// GH: EOF handling. Handle EOF like end of line. Can happen with
|
||||
// encoded object streams that the object is at the end.
|
||||
@@ -159,20 +159,20 @@ func (this *PSParser) parseNumber() (PSObject, error) {
|
||||
}
|
||||
if allowSigns && (bb[0] == '-' || bb[0] == '+') {
|
||||
// Only appear in the beginning, otherwise serves as a delimiter.
|
||||
b, _ := this.reader.ReadByte()
|
||||
b, _ := pP.reader.ReadByte()
|
||||
numStr += string(b)
|
||||
allowSigns = false // Only allowed in beginning, and after e (exponential).
|
||||
} else if pdfcore.IsDecimalDigit(bb[0]) {
|
||||
b, _ := this.reader.ReadByte()
|
||||
b, _ := pP.reader.ReadByte()
|
||||
numStr += string(b)
|
||||
} else if bb[0] == '.' {
|
||||
b, _ := this.reader.ReadByte()
|
||||
b, _ := pP.reader.ReadByte()
|
||||
numStr += string(b)
|
||||
isFloat = true
|
||||
} else if bb[0] == 'e' {
|
||||
// Exponential number format.
|
||||
// XXX Is this supported in PS?
|
||||
b, _ := this.reader.ReadByte()
|
||||
b, _ := pP.reader.ReadByte()
|
||||
numStr += string(b)
|
||||
isFloat = true
|
||||
allowSigns = true
|
||||
@@ -193,22 +193,22 @@ func (this *PSParser) parseNumber() (PSObject, error) {
|
||||
}
|
||||
|
||||
// Parse bool object.
|
||||
func (this *PSParser) parseBool() (*PSBoolean, error) {
|
||||
bb, err := this.reader.Peek(4)
|
||||
func (pP *PSParser) parseBool() (*PSBoolean, error) {
|
||||
bb, err := pP.reader.Peek(4)
|
||||
if err != nil {
|
||||
return MakeBool(false), err
|
||||
}
|
||||
if (len(bb) >= 4) && (string(bb[:4]) == "true") {
|
||||
this.reader.Discard(4)
|
||||
pP.reader.Discard(4)
|
||||
return MakeBool(true), nil
|
||||
}
|
||||
|
||||
bb, err = this.reader.Peek(5)
|
||||
bb, err = pP.reader.Peek(5)
|
||||
if err != nil {
|
||||
return MakeBool(false), err
|
||||
}
|
||||
if (len(bb) >= 5) && (string(bb[:5]) == "false") {
|
||||
this.reader.Discard(5)
|
||||
pP.reader.Discard(5)
|
||||
return MakeBool(false), nil
|
||||
}
|
||||
|
||||
@@ -216,10 +216,10 @@ func (this *PSParser) parseBool() (*PSBoolean, error) {
|
||||
}
|
||||
|
||||
// An operand is a text command represented by a word.
|
||||
func (this *PSParser) parseOperand() (*PSOperand, error) {
|
||||
func (pP *PSParser) parseOperand() (*PSOperand, error) {
|
||||
bytes := []byte{}
|
||||
for {
|
||||
bb, err := this.reader.Peek(1)
|
||||
bb, err := pP.reader.Peek(1)
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
@@ -233,7 +233,7 @@ func (this *PSParser) parseOperand() (*PSOperand, error) {
|
||||
break
|
||||
}
|
||||
|
||||
b, _ := this.reader.ReadByte()
|
||||
b, _ := pP.reader.ReadByte()
|
||||
bytes = append(bytes, b)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user