sourcemap

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mr Shore

    sourcemap


    has any one any idea about this applet?
    code as below:
    /**
    * SourceMap class,
    * reads a generic language source code and returns its map.
    * _______________ _______________ _______________ _______________ __
    * The SourceMap goals is to create a map of a generic script/program
    language.
    * The getMap method returns an array/list of arrays/dictionary/
    objects
    * of source map using delimeters variable to map correctly:
    * - multi line comments
    * - single line comments
    * - double quoted strings
    * - single quoted strings
    * - pure code
    * - everything else (for example regexp [/re/] with javascript),
    just adding a correct delimeter
    * --------------------------------------------------------------
    * What about the delimeter
    * It's an array/list of arrays/dictionary/obects with some
    properties to find what you're looking for.
    *
    * parameters are:
    * - name, the name of the delimeter (i.e. "doublequot e")
    * - start, one or mode chars to find as start delimeter (i.e. " for
    double quoted string)
    * - end, one or mode chars to find as end delimeter (i.e. " for
    double quoted string) [end should be an array/list too]
    *
    * optional parameters are:
    * - noslash, if true find the end of the delimeter only if last char
    is not slashed (i.e. "string\"te st" find " after test)
    * - match, if choosed language has regexp, verify if string from
    start to end matches used regexp (i.e. /^\/[^\n\r]+\/$/ for JavaScript
    regexp)
    *
    * If end parameter is an array, match and noslash are not supported
    (i.e. ["\n", "\r"] for end delimeter of a single line comment)
    * --------------------------------------------------------------
    * What about SourceMap usage
    * It should be a good solution to create sintax highlighter, parser,
    * verifier or some other source code parsing procedure
    * --------------------------------------------------------------
    * What about SourceMap performance script/languages
    * I've created different version of this class to test each script/
    program language performance too.
    * Python with or without Psyco is actually the faster parser.
    * --------------------------------------------------------------
    * @Compatibility >= JavaScript 1.1
    * @Author Andrea Giammarchi
    * @Site http://www.devpro.it/
    * @Date 2006/08/01
    * @LastMOd 2006/08/01
    * @Version 0.1
    */
    function SourceMap() {

    /**
    * public method
    * getMap(source:s tring, delimeters:arra y):array
    * Maps the source code using $delimeters rules and returns map as an
    array
    * NOTE: read comments to know more about map and delimeter
    *
    * @param string generic source code
    * @param array array with nested objects with code rules
    */
    this.getMap = function(source , delimeters) {

    // "unsigned integer" variables
    var sourcePosition = 0,
    delimetersPosit ion = 0,
    findLength = 0,
    len = 0,
    tempIndex = 0,
    sourceLength = source.length,
    delimetersLengt h = delimeters.leng th;

    // integer variables
    var tempPosition = -1,
    endPosition = -1;

    // array variables
    var map = [],
    tempMap = [];

    // object variable
    var tempDelimeter = {};

    while(sourcePos ition < sourceLength) {
    endPosition = -1;
    for(delimetersP osition = 0; delimetersPosit ion < delimetersLengt h;
    delimetersPosit ion++) {
    tempPosition =
    source.indexOf( delimeters[delimetersPosit ion].start, sourcePosition) ;
    if(tempPosition !== -1 && (tempPosition < endPosition ||
    endPosition === -1)) {
    endPosition = tempPosition;
    tempIndex = delimetersPosit ion;
    }
    }
    if(endPosition !== -1) {
    sourcePosition = endPosition;
    tempDelimeter = delimeters[tempIndex];
    findLength = tempDelimeter.s tart.length;
    if(tempDelimete r.end.construct or === Array) {
    delimetersPosit ion = 0;
    endPosition = -1;
    for(len = tempDelimeter.e nd.length; delimetersPosit ion < len;
    delimetersPosit ion++) {
    tempPosition =
    source.indexOf( tempDelimeter.e nd[delimetersPosit ion], sourcePosition +
    findLength);
    if(tempPosition !== -1 && (tempPosition < endPosition ||
    endPosition === -1)) {
    endPosition = tempPosition;
    tempIndex = delimetersPosit ion;
    }
    }
    if(endPosition !== -1)
    endPosition = endPosition + tempDelimeter.e nd[tempIndex].length;
    else
    endPosition = sourceLength;
    map.push({name: tempDelimeter.n ame, start:sourcePos ition,
    end:endPosition });
    sourcePosition = endPosition - 1;
    }
    else if(tempDelimete r.match) {
    tempPosition = source.indexOf( tempDelimeter.e nd, sourcePosition +
    findLength);
    len = tempDelimeter.e nd.length;
    if(tempPosition !== -1 &&
    tempDelimeter.m atch.test(sourc e.substr(source Position, tempPosition -
    sourcePosition + len))) {
    endPosition = tempDelimeter.n oslash ? __endCharNoSlas h(source,
    sourcePosition, tempDelimeter.e nd, sourceLength) : tempPosition + len;
    map.push({name: tempDelimeter.n ame, start:sourcePos ition,
    end:endPosition });
    sourcePosition = endPosition - 1;
    }
    }
    else {
    if(tempDelimete r.noslash)
    endPosition = __endCharNoSlas h(source, sourcePosition,
    tempDelimeter.e nd, sourceLength);
    else {
    tempPosition = source.indexOf( tempDelimeter.e nd, sourcePosition
    + findLength);
    if(tempPosition !== -1)
    endPosition = tempPosition + tempDelimeter.e nd.length;
    else
    endPosition = sourceLength;
    }
    map.push({name: tempDelimeter.n ame, start:sourcePos ition,
    end:endPosition });
    sourcePosition = endPosition - 1;
    }
    }
    else
    sourcePosition = sourceLength - 1;
    ++sourcePositio n;
    }
    len = map.length;
    if(len === 0)
    tempMap.push({n ame:'code', start:0, end:sourceLengt h});
    else {
    for(tempIndex = 0; tempIndex < len; tempIndex++) {
    if(tempIndex === 0 && map[tempIndex].start 0)
    tempMap.push({n ame:'code', start:0, end:map[tempIndex].start});
    else if(tempIndex 0 && map[tempIndex].start >
    map[tempIndex-1].end)
    tempMap.push({n ame:'code', start:map[tempIndex-1].end,
    end:map[tempIndex].start});
    tempMap.push({n ame:map[tempIndex].name,
    start:map[tempIndex].start, end:map[tempIndex].end});
    if(tempIndex + 1 === len && map[tempIndex].end < sourceLength)
    tempMap.push({n ame:'code', start:map[tempIndex].end,
    end:sourceLengt h});
    }
    }
    return tempMap;
    };

    function __endCharNoSlas h(source, position, find, len) {
    var temp = find.length;
    do {
    position = source.indexOf( find, position + 1);
    }while(position !== -1 && !__charNoSlash( source, position));
    if(position === -1) position = len - temp;
    return position + temp;
    };

    function __charNoSlash(s ource, position) {
    var next = 1,
    len = position - next;
    while(len 0 && source.charAt(l en) === '\\') len = position - (+
    +next);
    return ((next - 1) % 2 === 0);
    };
    };
  • Lasse Reichstein Nielsen

    #2
    Re: sourcemap

    Mr Shore <shore.cloud@gm ail.comwrites:
    Probably. Can you be more specific?
    code as below:
    I've read the comments for the source code, and I still have no
    idea what the code is supposed to do.

    Personally, that's enough to turn me completely off using it for
    anything. If the author cannot explain what his code does, it's
    likely that he doesn't really understand what he's doing.
    Life is too short to understand uncommented, or unsuably commented,
    code.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Dr J R Stockton

      #3
      Re: sourcemap

      In comp.lang.javas cript message <05775e55-7161-4286-9dab-43a5a5e09d8f@m1
      g2000pre.google groups.com>, Mon, 14 Apr 2008 12:54:34, Mr Shore
      <shore.cloud@gm ail.composted:
      >has any one any idea about this applet?
      See newsgroup FAQ, 2.3, para 5.

      <FAQENTRY>

      Insert, by hand, not using XML, "a) " before the first paragraph of FAQ
      2.3, "b) " before the second, etc.

      Before doing that, insert a paragraph break in para 5 before its last
      "M".

      Remove the last "ed" from 2.3.

      FAQ 4.20 : the third sentence does not make sense in the context
      provided by the first two. Move it to after the MoominBox. Simplify by
      inserting Snufkin() in the code box and adjusting the following
      paragraph.

      --
      (c) John Stockton, nr London, UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
      Web <URL:http://www.merlyn.demo n.co.uk/- FAQish topics, acronyms, & links.
      Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
      Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)

      Comment

      Working...