2015-08-23 102 views
-1

我想更好地理解遞歸下降解析器 - 特別是https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js。 我感到困惑的一個功能的目的:麻煩理解遞歸下降解析

next = function (c) { 

// If a c parameter is provided, verify that it matches the current character. 

      if (c && c !== ch) { 
       error("Expected '" + c + "' instead of '" + ch + "'"); 
      } 

// Get the next character. When there are no more characters, 
// return the empty string. 

      ch = text.charAt(at); 
      at += 1; 
      return ch; 
     }, 

可能有人請幫助我的理解?據我目前的理解(我可能是錯誤的),它會檢查參數(c)是否與字符串中的下一個字符不相同?如果是這樣,這是什麼意思? 任何幫助,將不勝感激。

回答

0

您只報告了下一個功能的一部分。以下是完整的身體:

 next = function (c) { 

// If a c parameter is provided, verify that it matches the current character. 

      if (c && c !== ch) { 
       error("Expected '" + c + "' instead of '" + ch + "'"); 
      } 

// Get the next character. When there are no more characters, 
// return the empty string. 

      ch = text.charAt(at); 
      at += 1; 
      return ch; 
     }, 

而且解釋是註釋:首先檢查(如果有的話)作爲參數傳遞的性格,是目前的字符串中。在此之後,無論如何,獲取輸入字符串的下一個字符。

+0

感謝您的回答。 因此if語句檢查參數(c)是否存在於整個字符串中,還是僅存在於字符串的其餘部分?我有點困惑,因爲'if(c && c!== ch)'似乎表明它正在檢查當前字符是否與下一個字符不相等? 感謝您的耐心等待。 –

+0

不,第一個測試是檢查一個字符是否已經真正傳遞給函數(如果你看其餘的代碼,你可以看到next可以被調用爲next()或者next (' - ')'或類似的)。 – Renzo