<commit_before>package cask

import "github.com/pkg/errors"

// A Parser represents the parser that uses the emitted token provided by Lexer.
type Parser struct {
	// cask specifies the parsed Cask.
	cask *Cask

	// lexer specifies Lexer pointer.
	lexer *Lexer

	// currentToken specifies the current emitted Lexer token.
	currentToken Token

	// peekToken specifies the next Lexer token.
	peekToken Token

	// errors specify an array of errors.
	errors []error

	// currentCaskVariant specifies the temporary cask Variant that currently
	// being parsed.
	currentCaskVariant *Variant

	// currentIfVariant specifies the temporary cask Variant that holds data
	// extracted in if condition.
	currentIfVariant *Variant
}

// NewParser creates a new Parser instance and returns its pointer. Requires a
// Lexer and a Cask to be specified as arguments.
func NewParser(lexer *Lexer) *Parser {
	p := &Parser{
		lexer:  lexer,
		errors: []error{},
	}

	// read two tokens, so both currentToken and peekToken are set
	p.nextToken()
	p.nextToken()

	return p
}

// parseVersion parses the version if the Parser.peekToken matches the cask
// requirements. If the ":latest" symbol is found, the version will become the
// "latest" string.
func (p *Parser) parseVersion() (string, error) {
	if p.peekTokenIs(STRING) {
		p.accept(STRING)
		return p.currentToken.Literal, nil
	}

	if p.peekTokenIs(SYMBOL) && p.peekToken.Literal == "latest" {
		p.accept(SYMBOL)
		return "latest", nil
	}

	return "", errors.New("version not found")
}

// parseAppcast parses the appcast if the Parser.peekToken matches the cask
// requirements. Supports both with and without checkpoint.
func (p *Parser) parseAppcast() (*Appcast, error) {
	if p.peekTokenIs(STRING) {
		p.accept(STRING)

		url := p.currentToken.Literal
		checkpoint := ""

		if p.peekTokenIs(COMMA) {
			p.accept(COMMA)
		}

		if p.peekTokenIs(NEWLINE) {
			p.accept(NEWLINE)
		}

		if p.peekTokenIs(IDENT) && p.peekToken.Literal == "checkpoint" {
			p.accept(IDENT)
			p.accept(SYMBOL)

			if p.peekTokenIs(STRING) {
				p.accept(STRING)
				checkpoint = p.currentToken.Literal
			}
		}

		return NewAppcast(url, checkpoint), nil
	}

	return nil, errors.New("appcast not found")
}

//
func (p *Parser) parseArtifact() (*Artifact, error) {
	switch p.currentToken.Literal {
	case "app":
		return p.parseArtifactApp()
	case "pkg":
		return p.parseArtifactPkg()
	case "binary":
		return p.parseArtifactBinary()
	default:
		return nil, errors.New("artifact not found")
	}
}

func (p *Parser) parseArtifactApp() (*Artifact, error) {
	if p.currentTokenIs(IDENT) && p.currentToken.Literal == "app" {
		if p.peekTokenIs(STRING) {
			p.accept(STRING)

			a := NewArtifact(ArtifactApp, p.currentToken.Literal)

			if p.peekTokenIs(COMMA) {
				p.accept(COMMA)
			}

			if p.peekTokenIs(NEWLINE) {
				p.accept(NEWLINE)
			}

			if p.peekTokenIs(IDENT) && p.peekToken.Literal == "target" {
				p.accept(IDENT)
				p.accept(SYMBOL)

				if p.peekTokenIs(STRING) {
					p.accept(STRING)
					a.Target = p.currentToken.Literal
				}
			}

			return a, nil
		}
	}

	return nil, errors.New(`error parsing "app" artifact`)
}

func (p *Parser) parseArtifactPkg() (*Artifact, error) {
	if p.currentTokenIs(IDENT) && p.currentToken.Literal == "pkg" {
		if p.peekTokenIs(STRING) {
			p.accept(STRING)

			a := NewArtifact(ArtifactApp, p.currentToken.Literal)

			if p.peekTokenIs(COMMA) {
				p.accept(COMMA)
			}

			if p.peekTokenIs(NEWLINE) {
				p.accept(NEWLINE)
			}

			if p.peekTokenIs(IDENT) && p.peekToken.Literal == "allow_untrusted" {
				p.accept(IDENT)
				p.accept(SYMBOL)

				if p.peekTokenIs(TRUE) {
					p.accept(TRUE)
					a.AllowUntrusted = true
				}
			}

			return a, nil
		}
	}

	return nil, errors.New(`error parsing "pkg" artifact`)
}

func (p *Parser) parseArtifactBinary() (*Artifact, error) {
	if p.currentTokenIs(IDENT) && p.currentToken.Literal == "binary" {
		if p.peekTokenIs(STRING) {
			p.accept(STRING)

			a := NewArtifact(ArtifactBinary, p.currentToken.Literal)

			if p.peekTokenIs(COMMA) {
				p.accept(COMMA)
			}

			if p.peekTokenIs(NEWLINE) {
				p.accept(NEWLINE)
			}<commit_msg>Add missing comments for Parser functions<commit_after>package cask

import "github.com/pkg/errors"

// A Parser represents the parser that uses the emitted token provided by Lexer.
type Parser struct {
	// cask specifies the parsed Cask.
	cask *Cask

	// lexer specifies Lexer pointer.
	lexer *Lexer

	// currentToken specifies the current emitted Lexer token.
	currentToken Token

	// peekToken specifies the next Lexer token.
	peekToken Token

	// errors specify an array of errors.
	errors []error

	// currentCaskVariant specifies the temporary cask Variant that currently
	// being parsed.
	currentCaskVariant *Variant

	// currentIfVariant specifies the temporary cask Variant that holds data
	// extracted in if condition.
	currentIfVariant *Variant
}

// NewParser creates a new Parser instance and returns its pointer. Requires a
// Lexer and a Cask to be specified as arguments.
func NewParser(lexer *Lexer) *Parser {
	p := &Parser{
		lexer:  lexer,
		errors: []error{},
	}

	// read two tokens, so both currentToken and peekToken are set
	p.nextToken()
	p.nextToken()

	return p
}

// parseVersion parses the version if the Parser.peekToken matches the cask
// requirements. If the ":latest" symbol is found, the version will become the
// "latest" string.
func (p *Parser) parseVersion() (string, error) {
	if p.peekTokenIs(STRING) {
		p.accept(STRING)
		return p.currentToken.Literal, nil
	}

	if p.peekTokenIs(SYMBOL) && p.peekToken.Literal == "latest" {
		p.accept(SYMBOL)
		return "latest", nil
	}

	return "", errors.New("version not found")
}

// parseAppcast parses the appcast if the Parser.peekToken matches the cask
// requirements. Supports both with and without checkpoint.
func (p *Parser) parseAppcast() (*Appcast, error) {
	if p.peekTokenIs(STRING) {
		p.accept(STRING)

		url := p.currentToken.Literal
		checkpoint := ""

		if p.peekTokenIs(COMMA) {
			p.accept(COMMA)
		}

		if p.peekTokenIs(NEWLINE) {
			p.accept(NEWLINE)
		}

		if p.peekTokenIs(IDENT) && p.peekToken.Literal == "checkpoint" {
			p.accept(IDENT)
			p.accept(SYMBOL)

			if p.peekTokenIs(STRING) {
				p.accept(STRING)
				checkpoint = p.currentToken.Literal
			}
		}

		return NewAppcast(url, checkpoint), nil
	}

	return nil, errors.New("appcast not found")
}

// parseArtifact parses the artifact if the Parser.currentToken matches one of
// the supported artifacts.
func (p *Parser) parseArtifact() (*Artifact, error) {
	switch p.currentToken.Literal {
	case "app":
		return p.parseArtifactApp()
	case "pkg":
		return p.parseArtifactPkg()
	case "binary":
		return p.parseArtifactBinary()
	default:
		return nil, errors.New("artifact not found")
	}
}

// parseArtifactApp parses the "app" artifact if the Parser.currentToken matches
// the requirements.
func (p *Parser) parseArtifactApp() (*Artifact, error) {
	if p.currentTokenIs(IDENT) && p.currentToken.Literal == "app" {
		if p.peekTokenIs(STRING) {
			p.accept(STRING)

			a := NewArtifact(ArtifactApp, p.currentToken.Literal)

			if p.peekTokenIs(COMMA) {
				p.accept(COMMA)
			}

			if p.peekTokenIs(NEWLINE) {
				p.accept(NEWLINE)
			}

			if p.peekTokenIs(IDENT) && p.peekToken.Literal == "target" {
				p.accept(IDENT)
				p.accept(SYMBOL)

				if p.peekTokenIs(STRING) {
					p.accept(STRING)
					a.Target = p.currentToken.Literal
				}
			}

			return a, nil
		}
	}

	return nil, errors.New(`error parsing "app" artifact`)
}

// parseArtifactPkg parses the "pkg" artifact if the Parser.currentToken matches
// the requirements.
func (p *Parser) parseArtifactPkg() (*Artifact, error) {
	if p.currentTokenIs(IDENT) && p.currentToken.Literal == "pkg" {
		if p.peekTokenIs(STRING) {
			p.accept(STRING)

			a := NewArtifact(ArtifactApp, p.currentToken.Literal)

			if p.peekTokenIs(COMMA) {
				p.accept(COMMA)
			}

			if p.peekTokenIs(NEWLINE) {
				p.accept(NEWLINE)
			}

			if p.peekTokenIs(IDENT) && p.peekToken.Literal == "allow_untrusted" {
				p.accept(IDENT)
				p.accept(SYMBOL)

				if p.peekTokenIs(TRUE) {
					p.accept(TRUE)
					a.AllowUntrusted = true
				}
			}

			return a, nil
		}
	}

	return nil, errors.New(`error parsing "pkg" artifact`)
}

// parseArtifactBinary parses the "binary" artifact if the Parser.currentToken
// matches the requirements.
func (p *Parser) parseArtifactBinary() (*Artifact, error) {
	if p.currentTokenIs(IDENT) && p.currentToken.Literal == "binary" {
		if p.peekTokenIs(STRING) {
			p.accept(STRING)

			a := NewArtifact(ArtifactBinary, p.currentToken.Literal)

			if p.peekTokenIs(COMMA) {
				p.accept(COMMA)
			}

			if p.peekTokenIs(NEWLINE) {
				p.accept(NEWLINE)
			}
{'fraction_non_alphanumeric': 0.12073310423825888, 'fraction_numerical': 0.0, 'mean_word_length': 3.5168132436627006, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1907111', 'n_tokens_mistral': 3408, 'n_tokens_neox': 2993, 'n_words': 781}