自己实现一个js框架(二)

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 ));
    }

转载于:https://my.oschina.net/u/1792175/blog/598042

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值