Skip to content

Commit

Permalink
remove usage of _.last
Browse files Browse the repository at this point in the history
relates to #68
  • Loading branch information
Shahar Soel committed Jan 4, 2016
1 parent 2be403c commit 2e7e859
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/parse/grammar/lookahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace chevrotain.lookahead {
checkForOrAmbiguities(alternativesTokens, orOccurrence, ruleGrammar)
}

let hasLastAnEmptyAlt = utils.isEmpty(_.last(alternativesTokens))
let hasLastAnEmptyAlt = utils.isEmpty(utils.last(alternativesTokens))
if (hasLastAnEmptyAlt) {
let lastIdx = alternativesTokens.length - 1
/**
Expand Down
8 changes: 4 additions & 4 deletions src/parse/parser_public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ namespace chevrotain {
let key = this.getKeyForAutomaticLookahead(prodName, prodKeys, prodOccurrence)
let firstAfterRepInfo = this.firstAfterRepMap.get(key)
if (firstAfterRepInfo === undefined) {
let currRuleName = _.last(this.RULE_STACK)
let currRuleName = utils.last(this.RULE_STACK)
let ruleGrammar = this.getGAstProductions().get(currRuleName)
let walker:interp.AbstractNextTerminalAfterProductionWalker = new nextToksWalker(ruleGrammar, prodOccurrence)
firstAfterRepInfo = walker.startWalking()
Expand Down Expand Up @@ -1586,7 +1586,7 @@ namespace chevrotain {

private getKeyForAutomaticLookahead(prodName:string, prodKeys:lang.HashTable<string>[], occurrence:number):string {
let occuMap = prodKeys[occurrence - 1]
let currRule = _.last(this.RULE_STACK)
let currRule = utils.last(this.RULE_STACK)
let key = occuMap[currRule]
if (key === undefined) {
key = prodName + occurrence + IN + currRule
Expand Down Expand Up @@ -1630,7 +1630,7 @@ namespace chevrotain {
occurrence:number,
laFuncBuilder:(number, any) => () => T,
extraArgs:any[] = []):() => T {
let ruleName = _.last(this.RULE_STACK)
let ruleName = utils.last(this.RULE_STACK)
let condition = <any>this.classLAFuncs.get(key)
if (condition === undefined) {
let ruleGrammar = this.getGAstProductions().get(ruleName)
Expand Down Expand Up @@ -1660,7 +1660,7 @@ namespace chevrotain {
private raiseNoAltException(occurrence:number, errMsgTypes:string):void {
let errSuffix = " but found: '" + this.NEXT_TOKEN().image + "'"
if (errMsgTypes === undefined) {
let ruleName = _.last(this.RULE_STACK)
let ruleName = utils.last(this.RULE_STACK)
let ruleGrammar = this.getGAstProductions().get(ruleName)
let nextTokens = new interp.NextInsideOrWalker(ruleGrammar, occurrence).startWalking()
let nextTokensFlat = utils.flatten(nextTokens)
Expand Down
9 changes: 7 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
considering they will not be invoked in hotspots...
*/

namespace utils {
namespace chevrotain.utils {

export function isEmpty(arr:any[]):boolean {
return arr.length === 0
return arr && arr.length === 0
}

export function keys(obj:any):string[] {
Expand Down Expand Up @@ -53,4 +53,9 @@ namespace utils {
export function first<T>(arr:T[]):T {
return isEmpty(arr) ? undefined : arr[0]
}

export function last<T>(arr:T[]):T {
let len = arr && arr.length
return len ? arr[len - 1] : undefined
}
}
12 changes: 12 additions & 0 deletions test/utils/utils_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace chevrotain.utils.spec {

describe("The Utils functions namespace", () => {

it("exports a last utility", () => {
expect(last([1, 2, 3])).to.equal(3)
expect(last([])).to.equal(undefined)
expect(last(null)).to.equal(undefined)
})
})

}

0 comments on commit 2e7e859

Please sign in to comment.