Javascript parser

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Robichaud

    #1

    Javascript parser

    Hi, I would like to know if there exists a tool that one can use to test
    some javascript code before actually running it ? Something that would check
    the syntax at least ? I found that most mistakes that I make when coding in
    javascript are syntax-related.

    Here's a typical scenario: I use a function like getElementByID. Notice the
    big D...In Internet Explorer I get the classic "object doesn't support this
    property or method" message, line X, char X that are of no use because they
    point somewhere I can't relate. Such an error could have been easily spotted
    with a parser or compiler.


  • bumbleguppy@hotmail.com

    #2
    Re: Javascript parser

    Download firefox.

    Firefox is a free web browser backed by Mozilla, a non-profit dedicated to internet health and privacy.


    In the menu when viewing your html page, go to Tools/Javascript
    Console.

    If you can't download firefox, try this:


    window.onerror= errHandler;
    var msgArray = new Array(),urlArra y = new Array(),lnoArra y = new
    Array();

    //paramenters of error functions(deter mined by default, not my
    idea...):
    //error message, url where error occurred and line number

    function errHandler(msg, url, lno) {
    msgArray[msgArray.length] = msg;
    urlArray[urlArray.length] = url;
    lnoArray[lnoArray.length] = lno;
    return true;
    }

    function displayErrors() {
    var s='Javascript Error Report\n\n\n';
    if(msgArray.len gth){
    for(var i=0;i<msgArray. length;i++){
    s+='line '+lnoArray[i]+':\n'+msgArray[i]+'\n\n';
    }
    alert(s);
    }
    }

    then just call 'displayErrors( )' where you want to see the report. The
    "return true" in the error handler will keep Internet Exporer from
    generating those annoying alert boxes (and if you have the C++ debugger
    installed, the debugger won't load either).

    Comment

    • Ivo

      #3
      Re: Javascript parser

      "Jeff Robichaud" wrote[color=blue]
      > Hi, I would like to know if there exists a tool that one can use to test
      > some javascript code before actually running it ? Something that would[/color]
      check[color=blue]
      > the syntax at least ?[/color]



      hth
      --
      Ivo


      Comment

      • Jeff Robichaud

        #4
        Re: Javascript parser

        Firefox javascript console: that will do the trick I think.
        jslint: not bad but still incomplete: it won't catch errors related to
        methods that are misspelled.

        Thank you guys!

        "Ivo" <no@thank.you > wrote in message
        news:4228b5b9$0 $61332$dbd41001 @news.wanadoo.n l...[color=blue]
        > "Jeff Robichaud" wrote[color=green]
        >> Hi, I would like to know if there exists a tool that one can use to test
        >> some javascript code before actually running it ? Something that would[/color]
        > check[color=green]
        >> the syntax at least ?[/color]
        >
        > http://www.crockford.com/javascript/jslint.html
        >
        > hth
        > --
        > Ivo
        >
        >[/color]


        Comment

        • Ivo

          #5
          Re: Javascript parser

          "Jeff Robichaud" wrote[color=blue]
          > "Ivo" wrote[color=green]
          > > "Jeff Robichaud" wrote[color=darkred]
          > >> Hi, I would like to know if there exists a tool that one can use to[/color][/color][/color]
          test[color=blue][color=green][color=darkred]
          > >> some javascript code before actually running it ? Something that would
          > >> check the syntax at least ?[/color]
          > >
          > > http://www.crockford.com/javascript/jslint.html[/color]
          >
          > Firefox javascript console: that will do the trick I think.
          > jslint: not bad but still incomplete: it won't catch errors related to
          > methods that are misspelled.[/color]

          Of course not, that would be very bad! How can jslint possibly know you have
          not written your own getElementByID with capital D? A word is only
          misspelled in the eye of the beholder. Expecting these things is not
          understanding the underlying mechanism. Really, jslint is much more
          informative than any browser js console if you know how to read its reports.
          Calling it "not bad" is an insult, not only to its creator, but to the
          undoubtedly hundreds of us that use it every day, and admire it for its
          accuracy, speed, and usefullness. I encourage you to have another look at
          it, and keep trying a bit longer before jumping to conclusions.
          --
          Ivo


          Comment

          • Michael Winter

            #6
            Re: Javascript parser

            Jeff Robichaud wrote:

            [snip]
            [color=blue]
            > Here's a typical scenario: I use a function like getElementByID.[/color]

            The getElementById method, as with any host object or property, has
            nothing to do with the language itself. It's not a syntax error, just
            a typo. Expecting a lint tool to correct errors like that is unrealistic.

            [snip]

            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail.

            Comment

            • Douglas Crockford

              #7
              Re: Javascript parser

              >> Here's a typical scenario: I use a function like getElementByID.
              [color=blue]
              > The getElementById method, as with any host object or property, has
              > nothing to do with the language itself. It's not a syntax error, just a
              > typo. Expecting a lint tool to correct errors like that is unrealistic.[/color]

              Suppose that JSLINT's report contained a list of all of the member names
              that it sees. It might be easier to spot a misspelled name in that list
              than in the source. What do you think?


              Comment

              • Michael Winter

                #8
                Re: Javascript parser

                Douglas Crockford wrote:

                [snip]
                [color=blue]
                > Suppose that JSLINT's report contained a list of all of the member names
                > that it sees. It might be easier to spot a misspelled name in that list
                > than in the source. What do you think?[/color]

                A rhetorical question I think, but yes, it might. [Just thinking aloud
                now, and briefly - time for sleep. I'm sure you've thought of what I'm
                about to write.]

                Assuming a sorted list and mistakes in the middle, or at the end, of
                member names, this would place duplicates side-by-side for one-off
                errors. If it was simple to match nearly-identical strings, that
                condition could be flagged as a warning. It could be implemented for
                strings that only differ by case.

                It wouldn't help if all occurrences (including single uses) were
                erroneous. Its utility might also be reduced in OO-heavy code which
                would produce a very long list.

                Of course, one could just try typing a little more carefully. :P

                Mike

                --
                Michael Winter
                Replace ".invalid" with ".uk" to reply by e-mail.

                Comment

                • Randy Webb

                  #9
                  Re: Javascript parser

                  Michael Winter wrote:[color=blue]
                  > Douglas Crockford wrote:
                  >
                  > [snip]
                  >[color=green]
                  >> Suppose that JSLINT's report contained a list of all of the member
                  >> names that it sees. It might be easier to spot a misspelled name in
                  >> that list than in the source. What do you think?[/color]
                  >
                  >
                  > A rhetorical question I think, but yes, it might. [Just thinking aloud
                  > now, and briefly - time for sleep. I'm sure you've thought of what I'm
                  > about to write.]
                  >
                  > Assuming a sorted list and mistakes in the middle, or at the end, of
                  > member names, this would place duplicates side-by-side for one-off
                  > errors. If it was simple to match nearly-identical strings, that
                  > condition could be flagged as a warning. It could be implemented for
                  > strings that only differ by case.
                  >
                  > It wouldn't help if all occurrences (including single uses) were
                  > erroneous. Its utility might also be reduced in OO-heavy code which
                  > would produce a very long list.
                  >
                  > Of course, one could just try typing a little more carefully. :P[/color]

                  What if it only gave you a list of unknown variable/member names? That
                  might, or might not, make things easier to notice. If it disregarded
                  getElementById but showed you getELementById, then you would know to
                  look for it.

                  --
                  Randy
                  comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

                  Comment

                  • Douglas Crockford

                    #10
                    Re: Javascript parser

                    >>> Suppose that JSLINT's report contained a list of all of the member[color=blue][color=green][color=darkred]
                    >>> names that it sees. It might be easier to spot a misspelled name in
                    >>> that list than in the source. What do you think?[/color][/color][/color]
                    [color=blue][color=green]
                    >> A rhetorical question I think, but yes, it might. [Just thinking aloud
                    >> now, and briefly - time for sleep. I'm sure you've thought of what I'm
                    >> about to write.]
                    >>
                    >> Assuming a sorted list and mistakes in the middle, or at the end, of
                    >> member names, this would place duplicates side-by-side for one-off
                    >> errors. If it was simple to match nearly-identical strings, that
                    >> condition could be flagged as a warning. It could be implemented for
                    >> strings that only differ by case.
                    >>
                    >> It wouldn't help if all occurrences (including single uses) were
                    >> erroneous. Its utility might also be reduced in OO-heavy code which
                    >> would produce a very long list.
                    >>
                    >> Of course, one could just try typing a little more carefully. :P[/color][/color]
                    [color=blue]
                    > What if it only gave you a list of unknown variable/member names? That
                    > might, or might not, make things easier to notice. If it disregarded
                    > getElementById but showed you getELementById, then you would know to
                    > look for it.[/color]

                    What is known or unknown can vary from platform to platform. I could
                    allow specification of known members with a /*members */ comment,
                    similar to the /*global */ comment.


                    Comment

                    • Ivo

                      #11
                      Re: Javascript parser

                      "Randy Webb" wrote[color=blue]
                      > Michael Winter wrote:[color=green]
                      > > Douglas Crockford wrote:
                      > >[color=darkred]
                      > >> Suppose that JSLINT's report contained a list of all of the member
                      > >> names that it sees. It might be easier to spot a misspelled name in
                      > >> that list than in the source. What do you think?[/color]
                      > >
                      > > Of course, one could just try typing a little more carefully. :P[/color]
                      >
                      > getELementById[/color]

                      Ah, now there 's a nasty one! But serious, a lint program should not care
                      about wrong and right. It may produce all sorts of lists, and this sounds
                      like a *very* useful one, not only for finding typo's, but to classify the
                      items is and must remain up to the user I think. The code on your site is
                      Tamil to me, because it unlike the majority it concerns itself with what
                      works and not with what doesn't. Javascript is so flexible, I can program my
                      own getELementById method, so I expect jslint to allow it too.
                      --
                      Ivo


                      Comment

                      • Randy Webb

                        #12
                        Re: Javascript parser

                        Ivo wrote:[color=blue]
                        > "Randy Webb" wrote
                        >[color=green]
                        >>Michael Winter wrote:
                        >>[color=darkred]
                        >>>Douglas Crockford wrote:
                        >>>
                        >>>
                        >>>>Suppose that JSLINT's report contained a list of all of the member
                        >>>>names that it sees. It might be easier to spot a misspelled name in
                        >>>>that list than in the source. What do you think?
                        >>>
                        >>>Of course, one could just try typing a little more carefully. :P[/color]
                        >>
                        >>getELementByI d[/color]
                        >
                        >
                        > Ah, now there 's a nasty one! But serious, a lint program should not care
                        > about wrong and right.[/color]

                        The idea, mine anywany, wasn't one of "this is wrong" but of "this isn't
                        a known item to me" but as Doublas pointed out that list of known and
                        unknown can, and is, different across platforms.

                        <snip>
                        [color=blue]
                        > Javascript is so flexible, I can program my own getELementById method,
                        > so I expect jslint to allow it too.[/color]

                        And it should. But seeing getELementById in a list of "unknowns" would
                        key a beginner, and even non-beginners, that it might be a typo.


                        --
                        Randy
                        comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

                        Comment

                        • Dr John Stockton

                          #13
                          Re: Javascript parser

                          JRS: In article <a2a84$4228e570 $43656451$27460 @msgid.meganews servers.co
                          m>, dated Fri, 4 Mar 2005 14:47:09, seen in news:comp.lang. javascript,
                          Douglas Crockford <nospam@covad.n et> posted :[color=blue][color=green][color=darkred]
                          >>> Here's a typical scenario: I use a function like getElementByID.[/color][/color]
                          >[color=green]
                          >> The getElementById method, as with any host object or property, has
                          >> nothing to do with the language itself. It's not a syntax error, just a
                          >> typo. Expecting a lint tool to correct errors like that is unrealistic.[/color]
                          >
                          >Suppose that JSLINT's report contained a list of all of the member names
                          >that it sees. It might be easier to spot a misspelled name in that list
                          >than in the source. What do you think?[/color]


                          Agreed.


                          Errors in case are evidently fairly common and hard to spot, as we see
                          in this group. For example, onKeypress is a perfectly legitimate name;
                          nevertheless, it is probably a typo, and otherwise almost certainly
                          injudicious.


                          The number of names already known to the system is finite, though large;
                          but a list covering well over 99% of instances-of-use would not be very
                          long, and could probably be made by RegExp processing of existing
                          javascript documentation, sorting, de-duplicating, and hand-polishing.
                          The following line looks semi-promising as a start, mtr being MiniTrue
                          and DEDUPE being mine :-

                          mtr -o^ -x+ d:\jscrrefs\jsr ef\jsr13\*.htm ".*<code>(\ w*)</code>.*" =
                          \1\r | sort | DEDUPE > file.txt

                          Or there might be a way of getting the list from a browser, or from
                          browser source code.

                          It could, IMHO, be useful to report as permissible but suspect any
                          instances of names that matched a well-known name case-independently but
                          differed in case.


                          It would also be useful to report instances of member names that were
                          duplicated but with case-differences, with a user-selectable parameter
                          for length (pragmatically, a page may well contain a J loop in one
                          routine and a j loop in another; but having a name Dustbin and another
                          DustBin should be viewed with suspicion [ either both should be
                          identical, or, for easier reading, one might become Dustpan ] ).


                          Perhaps the best would be to list all distinct names, sorted independent
                          of case, and annotated symbolically; a user could then process the list
                          either by eye or by code. Maybe in Column 1 a # for any name that
                          matches anything but only when case is ignored, else space; in Column 3
                          a % for a match with a "predefined " name; ...; in Column 2N+1 put the
                          name itself. Perhaps a column of frequency-count:

                          # % 2 gettimeout
                          # 15 getToadStool
                          # 1 getToadstool
                          1 getTodeStool

                          and it is also clear which are probably wrong.

                          H'mmm - and any name that is not "predefined " and is used only once is
                          also, IMHO, worth considering, though possibly legitimate especially in
                          a page with "working but incomplete" status.


                          IIRC, running jslint kills MSIE4; I consider that a superfluous effect,
                          perhaps removable by feature detection and/or substitution.

                          --
                          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                          Web <URL:http://www.merlyn.demo n.co.uk/> - FAQish topics, acronyms, & links.
                          I find MiniTrue useful for viewing/searching/altering files, at a DOS prompt;
                          free, DOS/Win/UNIX, <URL:http://www.idiotsdelig ht.net/minitrue/> Update hope?

                          Comment

                          • Csaba2000

                            #14
                            Re: Javascript parser


                            "Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote in message
                            news:cSIbYQBj4b KCFwQK@merlyn.d emon.co.uk...[color=blue]
                            > JRS: In article <a2a84$4228e570 $43656451$27460 @msgid.meganews servers.co
                            > m>, dated Fri, 4 Mar 2005 14:47:09, seen in news:comp.lang. javascript,
                            > Douglas Crockford <nospam@covad.n et> posted :[color=green][color=darkred]
                            >>>> Here's a typical scenario: I use a function like getElementByID.[/color]
                            >>[color=darkred]
                            >>> The getElementById method, as with any host object or property, has
                            >>> nothing to do with the language itself. It's not a syntax error, just a
                            >>> typo. Expecting a lint tool to correct errors like that is unrealistic.[/color]
                            >>
                            >>Suppose that JSLINT's report contained a list of all of the member names
                            >>that it sees. It might be easier to spot a misspelled name in that list
                            >>than in the source. What do you think?[/color]
                            >
                            > The number of names already known to the system is finite, though large;
                            > but a list covering well over 99% of instances-of-use would not be very
                            > long, and could probably be made by RegExp processing of existing
                            > javascript documentation, sorting, de-duplicating, and hand-polishing.
                            > The following line looks semi-promising as a start, mtr being MiniTrue
                            > and DEDUPE being mine :-
                            >
                            > mtr -o^ -x+ d:\jscrrefs\jsr ef\jsr13\*.htm ".*<code>(\ w*)</code>.*" =
                            > \1\r | sort | DEDUPE > file.txt
                            >
                            > Or there might be a way of getting the list from a browser, or from
                            > browser source code.[/color]

                            Dr. Stockton's post makes some excellent points, which I'd like to echo.
                            Probably, the number of "reserved words" across all platforms is no
                            more than several hundred, say 1000. This is not so very large and
                            I would think that in the majority of cases, being off in case (or being
                            one letter too many or too few) might be due cause for noting.
                            Possibly a: similar to getElementById
                            type of comment or color indicator (which just possibly, maybe,
                            might be suppressable on a word by word basis).

                            Checking for case difference is easy. Testing for one offness is
                            not so trivial. If I were going to do this, I'd have a dictionary
                            of lowercase keywords (values (possibly arrays) are the the
                            actual keywords). When you want to check a user's
                            supplied entry, lowercase it and check it against the dictionary.
                            Now you could try all the different combinations of removing
                            exactly one letter from the user supplied entry to tell whether
                            the user supplied entry is one letter too big.

                            To check for the user entry being one letter too small, I
                            would do this same trick only using the original keywords.
                            In other words, for getElementById have dictionary
                            entries for:
                            getelementbyid, etelementbyid, gtelementbyid, geelementbyid,
                            getlementbyid, geteementbyid, ...

                            I figure your dictionary wouldn't have more than about 10000
                            entries.

                            Csaba Gabor from Vienna


                            Comment

                            • Douglas Crockford

                              #15
                              Re: Javascript parser

                              >>>Suppose that JSLINT's report contained a list of all of the member names[color=blue][color=green][color=darkred]
                              >>>that it sees. It might be easier to spot a misspelled name in that list
                              >>>than in the source. What do you think?[/color]
                              >>
                              >>The number of names already known to the system is finite, though large;
                              >>but a list covering well over 99% of instances-of-use would not be very
                              >>long, and could probably be made by RegExp processing of existing
                              >>javascript documentation, sorting, de-duplicating, and hand-polishing.
                              >>The following line looks semi-promising as a start, mtr being MiniTrue
                              >>and DEDUPE being mine :-
                              >>
                              >>mtr -o^ -x+ d:\jscrrefs\jsr ef\jsr13\*.htm ".*<code>(\ w*)</code>.*" =
                              >>\1\r | sort | DEDUPE > file.txt
                              >>
                              >>Or there might be a way of getting the list from a browser, or from
                              >>browser source code.[/color]
                              >
                              >
                              > Dr. Stockton's post makes some excellent points, which I'd like to echo.
                              > Probably, the number of "reserved words" across all platforms is no
                              > more than several hundred, say 1000. This is not so very large and
                              > I would think that in the majority of cases, being off in case (or being
                              > one letter too many or too few) might be due cause for noting.
                              > Possibly a: similar to getElementById
                              > type of comment or color indicator (which just possibly, maybe,
                              > might be suppressable on a word by word basis).
                              >
                              > Checking for case difference is easy. Testing for one offness is
                              > not so trivial. If I were going to do this, I'd have a dictionary
                              > of lowercase keywords (values (possibly arrays) are the the
                              > actual keywords). When you want to check a user's
                              > supplied entry, lowercase it and check it against the dictionary.
                              > Now you could try all the different combinations of removing
                              > exactly one letter from the user supplied entry to tell whether
                              > the user supplied entry is one letter too big.
                              >
                              > To check for the user entry being one letter too small, I
                              > would do this same trick only using the original keywords.
                              > In other words, for getElementById have dictionary
                              > entries for:
                              > getelementbyid, etelementbyid, gtelementbyid, geelementbyid,
                              > getlementbyid, geteementbyid, ...
                              >
                              > I figure your dictionary wouldn't have more than about 10000
                              > entries.[/color]

                              10000 might be a bit of a download. JSLINT now lists all member names.
                              Please try it. Let me know if it is useful, or if more is required.


                              Comment

                              Working...