Changeset 10603 for lang/actionscript

Show
Ignore:
Timestamp:
04/27/08 23:38:22 (5 years ago)
Author:
gyuque
Message:

grammar auto converter(now in progress...)

Location:
lang/actionscript/ascss/src/css
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/actionscript/ascss/src/css/convert-y.rb

    r10596 r10603  
    22 
    33class LexPattern 
    4         def initialize(c, rx, name) 
     4        def initialize(c, rx, name, back) 
     5                @back = back 
    56                @chkey = c 
    67                @rx = rx 
     
    89        end 
    910 
     11        attr_reader :back 
    1012        attr_reader :chkey 
    1113        attr_reader :rx 
     
    5355                LX('p', "preprocessor",  /\A\##{nm_start}#{nm_chars}/) 
    5456                LX('d', "decimal",       /\A#{digits}+/) 
    55                 LX('t', "static_cast",   /\Astatic_cast/) 
    56                 LX('D', "delete",        /\Adelete/) 
     57                LX('P', "primitive_type",/\A(int|short)[^a-zA-Z0-9_]/, 1) 
     58                LX('t', "static_cast",   /\Astatic_cast[^a-zA-Z0-9_]/, 1) 
     59                LX('D', "delete",        /\Adelete[^a-zA-Z0-9_]/, 1) 
    5760                LX('i', "ident",         /\A#{nm_start}#{nm_chars}/) 
    5861                LX('v', "y_variable",    /\A\$[1-9$]/) 
     
    6467        end 
    6568 
    66         def LX(c, name, rx) 
     69        def LX(c, name, rx, back = 0) 
    6770                if c != nil 
    6871                        raise "duplicated char key '#{c}'" if @chmap.key?(c) 
     
    7174                end 
    7275 
    73                 @patterns << LexPattern.new(c, rx, name) 
     76                @patterns << LexPattern.new(c, rx, name, back) 
    7477        end 
    7578 
     
    7780                @patterns.each{|ptn| 
    7881                        if @current =~ ptn.rx 
    79                                 tokbody = $& 
     82                                tokbody = $&[0, $&.length - ptn.back] 
    8083                                @current = @current[tokbody.length, @current.length] 
    8184 
     
    9295                        @patterns.each{|ptn| 
    9396                                if s =~ ptn.rx 
    94                                         tokbody = $& 
     97                                        tokbody = $&[0, $&.length - ptn.back] 
    9598                                        s = s[tokbody.length, s.length] 
    9699 
  • lang/actionscript/ascss/src/css/parse-y.rb

    r10596 r10603  
    3030        end 
    3131 
     32        def inject_toks(tks) 
     33                tks.each{|t| @consumed_toks << t} 
     34        end 
     35 
    3236        def stop_save 
    3337                @dont_save = true 
     
    5357                return false if empty? 
    5458 
    55                 if @keystr =~ /^(S) *\{[ Li;*n<>]*\}/ 
     59                if @keystr =~ /^(S) *\{[ Li;*n<>P]*\}/ 
    5660                # %union { ... } 
    5761                        consume($&.length) 
     
    6872                        consume($&.length) 
    6973                else 
     74                        10.times do |i| 
     75                        STDERR.print @toks[i].body 
     76                        end 
     77 
    7078                        raise "bad token" 
    7179                end 
     
    9098 
    9199TYPE_MAP = { 
    92         "PseudoType" => "int" 
     100        "PseudoType" => "int", 
     101        "Function"   => "ParseFunction", 
     102        "CSSParser"  => "ASCSSParser" 
    93103} 
    94104 
     
    141151                                stop_save 
    142152                                consume(1) 
    143                                 process_program_block 
     153                                inject_toks( process_program_block ) 
    144154                                start_save 
    145155                        elsif @keystr =~ /^[;|]/ 
     
    169179 
    170180        def convert_code(toks) 
    171                 toks = filt_code(toks, /^D i(ai)? ?;/, "commentout_deleteop") 
    172                 toks = change_variable_declaration_nstype(toks) 
    173                 toks = change_variable_declaration(toks) 
    174                 toks = change_arrow_to_dot(toks) 
    175                 toks = change_namespace_to_class(toks) 
    176                 puts toks.map{|t| t.body}.join('') 
     181                toks = filt_code(toks, /^(v ?= ?)d/,       "change_zeronull") 
     182                toks = filt_code(toks, /^t<(i\*?)>\(i(\.i)?\)/,"change_static_cast") 
     183                toks = filt_code(toks, /^D i(ai)? ?;/,   "commentout_deleteop") 
     184                toks = filt_code(toks, /^(ini ?)i ?=/,   "change_variable_declaration_nstype") 
     185                toks = filt_code(toks, /^(i ?\* ?)i ?=/, "change_variable_declaration") 
     186                toks = filt_code(toks, /^P i ?=/,        "change_p_variable_declaration") 
     187                toks = filt_arrow_to_dot(toks) 
     188                toks = filt_code(toks, /^ini/,           "change_namespace_to_class") 
     189 
     190                toks 
    177191        end 
    178192 
     
    182196                loop do 
    183197                        if keys =~ ptn 
    184                                 keys = self.__send__(handler_name, ret, toks, keys, Regexp.last_match) 
    185                         else 
    186                                 ret << toks.shift 
    187                                 keys = keys[1,keys.length] 
    188                         end 
     198                                nextkey = self.__send__(handler_name, ret, toks, keys, Regexp.last_match) 
     199                                if nextkey != nil 
     200                                        keys = nextkey 
     201                                        next 
     202                                end 
     203                        end 
     204                         
     205                        ret << toks.shift 
     206                        keys = keys[1,keys.length] 
     207 
    189208                        break if keys.length < 1 
    190209                end 
     
    193212        end 
    194213 
     214        def change_zeronull(outtoks, intoks, keys, match) 
     215                 
     216 
     217                tokcount = match[0].length 
     218                pvarname = intoks[0].body 
     219                val      = intoks[match[1].length].body 
     220 
     221                return nil if pvarname != '$$' || val != '0' 
     222 
     223                new_toks = generate_token("#{pvarname} = null /* 0 */") 
     224 
     225                keys = keys[tokcount,keys.length] 
     226                tokcount.times do 
     227                        intoks.shift 
     228                end 
     229 
     230                new_toks.each{|t| outtoks << t} 
     231 
     232                return keys 
     233        end 
     234 
     235        def change_static_cast(outtoks, intoks, keys, match) 
     236                tokcount = match[0].length 
     237 
     238                casttype = intoks[2].body 
     239                varname  = intoks[match[1].length+4].body 
     240 
     241                cmt = "" 
     242                if TYPE_MAP.key?(casttype) 
     243                        cmt = " /* original: #{casttype} */" 
     244                        casttype = TYPE_MAP[casttype] 
     245                end 
     246 
     247                new_toks = generate_token("(#{varname} as #{casttype}#{cmt})") 
     248 
     249                keys = keys[tokcount,keys.length] 
     250                tokcount.times do 
     251                        intoks.shift 
     252                end 
     253 
     254                new_toks.each{|t| outtoks << t} 
     255 
     256                return keys 
     257        end 
     258 
    195259        def commentout_deleteop(outtoks, intoks, keys, match) 
    196260                tokcount = match[0].length 
     
    208272        end 
    209273 
    210         def change_variable_declaration_nstype(toks) 
    211                 ret = [] 
    212                 keys = make_tokkey_string(toks) 
    213  
    214                 loop do 
    215                         if keys =~ /^(ini ?)i ?=/ 
    216                                 tokcount = $&.length 
    217  
    218                                 vartype = toks[2].body 
    219                                 varname = toks[$1.length].body 
    220  
    221                                 cmt = "" 
    222                                 if TYPE_MAP.key?(vartype) 
    223                                         cmt = "/* typedefed #{vartype} */ " 
    224                                         vartype = TYPE_MAP[vartype] 
    225                                 end 
    226  
    227                                 new_toks = generate_token("var #{varname}:#{vartype} #{cmt}=") 
    228  
    229                                 keys = keys[tokcount,keys.length] 
    230                                 tokcount.times do 
    231                                         toks.shift 
    232                                 end 
    233  
    234                                 new_toks.each{|t| ret << t} 
    235                         else 
    236                                 ret << toks.shift 
    237                                 keys = keys[1,keys.length] 
    238                         end 
    239                         break if keys.length < 1 
    240                 end 
    241  
    242                 ret 
    243         end 
    244  
    245         def change_variable_declaration(toks) 
    246                 ret = [] 
    247                 keys = make_tokkey_string(toks) 
    248  
    249                 loop do 
    250                         if keys =~ /^(i ?\* ?)i ?=/ 
    251                                 tokcount = $&.length 
    252  
    253                                 vartype = toks[0].body 
    254                                 varname = toks[$1.length].body 
    255                                 new_toks = generate_token("var #{varname}:#{vartype} =") 
    256  
    257                                 keys = keys[tokcount,keys.length] 
    258                                 tokcount.times do 
    259                                         toks.shift 
    260                                 end 
    261  
    262                                 new_toks.each{|t| ret << t} 
    263                         else 
    264                                 ret << toks.shift 
    265                                 keys = keys[1,keys.length] 
    266                         end 
    267                         break if keys.length < 1 
    268                 end 
    269  
    270                 ret 
    271         end 
    272  
    273         def change_arrow_to_dot(toks) 
     274        def change_variable_declaration_nstype(outtoks, intoks, keys, match) 
     275                tokcount = match[0].length 
     276 
     277                vartype = intoks[2].body 
     278                varname = intoks[match[1].length].body 
     279 
     280                cmt = "" 
     281                if TYPE_MAP.key?(vartype) 
     282                        cmt = "/* original: #{vartype} */ " 
     283                        vartype = TYPE_MAP[vartype] 
     284                end 
     285 
     286                new_toks = generate_token("var #{varname}:#{vartype} #{cmt}=") 
     287 
     288                keys = keys[tokcount,keys.length] 
     289                tokcount.times do 
     290                        intoks.shift 
     291                end 
     292 
     293                new_toks.each{|t| outtoks << t} 
     294 
     295                return keys 
     296        end 
     297 
     298        def change_variable_declaration(outtoks, intoks, keys, match) 
     299                tokcount = match[0].length 
     300 
     301                vartype = intoks[0].body 
     302                varname = intoks[match[1].length].body 
     303 
     304                cmt = "" 
     305                if TYPE_MAP.key?(vartype) 
     306                        cmt = "/* original: #{vartype} */ " 
     307                        vartype = TYPE_MAP[vartype] 
     308                end 
     309 
     310                new_toks = generate_token("var #{varname}:#{vartype} #{cmt}=") 
     311 
     312                keys = keys[tokcount,keys.length] 
     313                tokcount.times do 
     314                        intoks.shift 
     315                end 
     316 
     317                new_toks.each{|t| outtoks << t} 
     318 
     319                return keys 
     320        end 
     321 
     322        def change_p_variable_declaration(outtoks, intoks, keys, match) 
     323                tokcount = match[0].length 
     324 
     325                vartype = intoks[0].body 
     326                varname = intoks[2].body 
     327 
     328                new_toks = generate_token("var #{varname}:#{vartype} =") 
     329 
     330                keys = keys[tokcount,keys.length] 
     331                tokcount.times do 
     332                        intoks.shift 
     333                end 
     334 
     335                new_toks.each{|t| outtoks << t} 
     336 
     337                return keys 
     338        end 
     339 
     340        def filt_arrow_to_dot(toks) 
    274341                toks.map{|t| (t.name == "arrow") ? Token.new(nil, '.', "single_char") : t } 
    275342        end 
    276343 
    277         def change_namespace_to_class(toks) 
    278                 ret = [] 
    279  
    280                 keys = make_tokkey_string(toks) 
    281                 loop do 
    282                         if keys =~ /^ini/ 
    283                                 ret << toks.shift 
    284  
    285                                 ret << Token.new(nil, '.', "single_char") 
    286                                 toks.shift # ignore '::' 
    287  
    288                                 ret << toks.shift 
    289                                 keys = keys[3,keys.length] 
    290                         else 
    291                                 ret << toks.shift 
    292                                 keys = keys[1,keys.length] 
    293                         end 
    294  
    295                         break if keys.length < 1 
    296                 end 
    297  
    298                 ret 
     344        def change_namespace_to_class(outtoks, intoks, keys, match) 
     345                outtoks << intoks.shift 
     346 
     347                outtoks << Token.new(nil, '.', "single_char") 
     348                intoks.shift # ignore '::' 
     349 
     350                outtoks << intoks.shift 
     351 
     352                return keys[3,keys.length] 
    299353        end 
    300354end 
     
    317371        end 
    318372 
    319         # puts psr.processed_src 
    320 end 
    321  
    322  
     373        puts psr.processed_src 
     374end 
     375 
     376