2011-10-29 11 views
5

. 'un ayrıntılarını neden yedeklemeliyim, ancak bunu yapmam gerekiyor, ancak 'un kütüphane kodunun çok büyük bir parçasını entegre etmek istemediğimi belirtmek yeterliydi ve bu benim için çok önemlidir. Bu çok hafif ve nispeten basit tutmak. Öyleyse lütfen JsLint'i veya bunun gibi herhangi bir şeyi kullanmamı önermeyin. Cevap, cevabınıza yapıştırdığınızdan daha fazla kod ise, muhtemelen daha fazla istiyorum.javascript kodunda bir javascript kodunun bir tür ham olarak ayrıştırılmasını yapıyorum javascript kodunun bir dizesinde düzenli ifade değişmezleri bulmak

Şu anda kodum, alıntılanan bölümleri ve yorumları algılamak ve ardından parantezleri, parantezleri ve parantezleri eşleştirmek (alıntılar ve yorumlar ile karıştırılmamasını veya alıntılar içinde kaçmadığından emin olmak) konusunda iyi bir iş çıkartabilmektedir. . Tek yapmam gereken tek şey şu: ve bu bir istisna var:

Düzenli ifade değişmezleriyle karıştırılabilir. Bu yüzden, bir javascript dizesinde düzenli ifade edebiyallerini tespit etmek için biraz yardım bekliyorum, bu yüzden onları uygun şekilde halledebilirim. Böyle

şey:

function getRegExpLiterals (stringOfJavascriptCode) { 
    var output = []; 
    // todo! 
    return output; 
} 

var jsString = "var regexp1 = /abcd/g, regexp1 = /efg/;" 
console.log (getRegExpLiterals (jsString)); 

// should print: 
// [{startIndex: 13, length: 7}, {startIndex: 32, length: 5}] 
+0

herhangi normal ifade sabitleri başlar belirler

biraz? Sadece içinde neler olduğunu istiyorsanız // yapmak çok kolay. – FailedDev

+0

Bir regex değişmez olduğundan emin olmalıyım, bu yüzden sadece eğik çizgi aramak bunu yapmayacaktır. – rob

cevap

5

es5-lexer da bölünme ifadeleri JS kodu düzenli ifadeler ayırt etmek çok doğru bir sezgisel bir yaklaşım kullanır ve bir JS lexer yapmak için kullanabileceğiniz bir belirteç düzeyi dönüşümü sağlar olduğu Sonuçta oluşan programın lexer tarafından olduğu gibi tam bir JS ayrıştırıcısı tarafından aynı şekilde yorumlanacağından emin olun. Bir / bir normal ifade guess_is_regexp.js içinde de başlar ve testler scanner_test.js line 401

var REGEXP_PRECEDER_TOKEN_RE = new RegExp(
    "^(?:" // Match the whole tokens below 
    + "break" 
    + "|case" 
    + "|continue" 
    + "|delete" 
    + "|do" 
    + "|else" 
    + "|finally" 
    + "|in" 
    + "|instanceof" 
    + "|return" 
    + "|throw" 
    + "|try" 
    + "|typeof" 
    + "|void" 
    // Binary operators which cannot be followed by a division operator. 
    + "|[+]" // Match + but not ++. += is handled below. 
    + "|-" // Match - but not --. -= is handled below. 
    + "|[.]" // Match . but not a number with a trailing decimal. 
    + "|[/]" // Match /, but not a regexp. /= is handled below. 
    + "|," // Second binary operand cannot start a division. 
    + "|[*]" // Ditto binary operand. 
    + ")$" 
    // Or match a token that ends with one of the characters below to match 
    // a variety of punctuation tokens. 
    // Some of the single char tokens could go above, but putting them below 
    // allows closure-compiler's regex optimizer to do a better job. 
    // The right column explains why the terminal character to the left can only 
    // precede a regexp. 
    + "|[" 
    + "!" // !   prefix operator operand cannot start with a division 
    + "%" // %   second binary operand cannot start with a division 
    + "&" // &, &&  ditto binary operand 
    + "(" // (   expression cannot start with a division 
    + ":" // :   property value, labelled statement, and operand of ?: 
      //    cannot start with a division 
    + ";" // ;   statement & for condition cannot start with division 
    + "<" // <, <<, << ditto binary operand 
    // !=, !==, %=, &&=, &=, *=, +=, -=, /=, <<=, <=, =, ==, ===, >=, >>=, >>>=, 
    // ^=, |=, ||= 
    // All are binary operands (assignment ops or comparisons) whose right 
    // operand cannot start with a division operator 
    + "=" 
    + ">" // >, >>, >>> ditto binary operand 
    + "?" // ?   expression in ?: cannot start with a division operator 
    + "[" // [   first array value & key expression cannot start with 
      //    a division 
    + "^" //^   ditto binary operand 
    + "{" // {   statement in block and object property key cannot start 
      //    with a division 
    + "|" // |, ||  ditto binary operand 
    + "}" // }   PROBLEMATIC: could be an object literal divided or 
      //    a block. More likely to be start of a statement after 
      //    a block which cannot start with a /. 
    + "~" // ~   ditto binary operand 
    + "]$" 
    // The exclusion of ++ and -- from the above is also problematic. 
    // Both are prefix and postfix operators. 
    // Given that there is rarely a good reason to increment a regular expression 
    // and good reason to have a post-increment operator as the left operand of 
    // a division (x++/y) this pattern treats ++ and -- as division preceders. 
); 
+0

Teşekkürler Mike, ileride tam lexer için kullanabilirim, bu etkileyici bir eserdir (ayrıca yazdığınız ve yazdıklarımı da kullandım.) – rob

+0

@rob, Rica ederim. Mutlu millet. –