2015-08-23 105 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