jQuery采用的sizzle引擎,实现了一个解释器,也就是DSL语言。
编译原理知识学习:
第一步:词法分析,先理解一个一个的词(words)和关键词,再是理解一句话
第二步:解析成一棵树
第三步:语义分析
词法分析器
词法分析器又称扫描器,词法分析是指将我们编写的文本代码流解析为一个一个的记号,分析得到的记号以供后续语法分析使用.
词法分析器的作用
- 移出多余的空白
- 切词
扫描器
好多编程语言其实有内置的扫描器,比如世界上最好的语言php
macros = {
'nl' : '\n|\r\n|\r|\f' ,
'nonascii' : '[^\0-\177]' ,
'unicode' : '\\[0-9A-Fa-f]{1,6}(\r\n|[\s\n\r\t\f])?' ,
'escape' : '#{unicode}|\\[^\n\r\f0-9A-Fa-f]' ,
'nmchar' : '[_A-Za-z0-9-]|#{nonascii}|#{escape}' ,
'nmstart' : '[_A-Za-z]|#{nonascii}|#{escape}' ,
'ident' : '[-@]?(#{nmstart})(#{nmchar})*' ,
'name' : '(#{nmchar})+'
};
rules = {
'id and name' : '(#{ident}##{ident})' ,
'id' : '(##{ident})' ,
'class' : '(\\.#{ident})' ,
'name and class' : '(#{ident}\\.#{ident})' ,
'element' : '(#{ident})' ,
'pseudo class' : '(:#{ident})'
};
处理过程
while (match = r.exec (this .selector )) {
finder = null ;
if (match [10 ]) {
finder = 'id' ;
} else if (match [1]) {
finder = 'name and id' ;
} else if (match [29 ]) {
finder = 'name' ;
} else if (match [15 ]) {
finder = 'class' ;
} else if (match [20 ]) {
finder = 'name and class' ;
}
this .tokens .push (new Token (match [0], finder ));
}