fix: noSelf

This commit is contained in:
2025-06-04 04:24:23 +09:00
parent 36384eee09
commit 5c75c3aafc
4661 changed files with 899200 additions and 5 deletions

View File

@@ -0,0 +1,20 @@
Copyright (c) Oskar Schöldström 2012-2014
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,274 @@
# luaparse [![Build Status](https://travis-ci.org/oxyc/luaparse.svg)](https://travis-ci.org/oxyc/luaparse)
A Lua parser written in JavaScript, for my bachelor's thesis at Arcada.
## Installation
Install through `bower install luaparse` or `npm install luaparse`.
## Usage
CommonJS
```js
var parser = require('luaparse');
var ast = parser.parse('i = 0');
console.log(JSON.stringify(ast));
```
AMD
```js
require(['luaparse'], function(parser) {
var ast = parser.parse('i = 0');
console.log(JSON.stringify(ast));
});
```
Browser
```html
<script src="luaparse.js"></script>
<script>
var ast = luaparse.parse('i = 0');
console.log(JSON.stringify(ast));
</script>
```
## Parser Interface
Basic usage:
```js
luaparse.parse(code, options);
```
The output of the parser is an Abstract Syntax Tree (AST) formatted in JSON.
The available options are:
- `wait: false` Explicitly tell the parser when the input ends.
- `comments: true` Store comments as an array in the chunk object.
- `scope: false` Track identifier scopes.
- `locations: false` Store location information on each syntax node.
- `ranges: false` Store the start and end character locations on each syntax
node.
- `onCreateNode: null` A callback which will be invoked when a syntax node
has been completed. The node which has been created will be passed as the
only parameter.
- `onCreateScope: null` A callback which will be invoked when a new scope is
created.
- `onDestroyScope: null` A callback which will be invoked when the current
scope is destroyed.
The default options are also exposed through `luaparse.defaultOptions` where
they can be overriden globally.
There is a second interface which might be preferable when using the `wait`
option.
```js
var parser = luaparse.parse({ wait: true });
parser.write('foo = "');
parser.write('bar');
var ast = parser.end('"');
```
This would be identical to:
```js
var ast = luaparse.parse('foo = "bar"');
```
### AST format
If the following code is executed:
```js
luaparse.parse('foo = "bar"');
```
then the returned value will be:
```js
{
"type": "Chunk",
"body": [
{
"type": "AssignmentStatement",
"variables": [
{
"type": "Identifier",
"name": "foo"
}
],
"init": [
{
"type": "StringLiteral",
"value": "bar",
"raw": "\"bar\""
}
]
}
],
"comments": []
}
```
### Custom AST
The default AST structure is somewhat inspired by the Mozilla Parser API but
can easily be overriden to customize the structure or to inject custom logic.
`luaparse.ast` is an object containing all functions used to create the AST, if
you for example wanted to trigger an event on node creations you could use the
following:
```js
var luaparse = require('luaparse'),
events = new (require('events').EventEmitter);
Object.keys(luaparse.ast).forEach(function(type) {
var original = luaparse.ast[type];
luaparse.ast[type] = function() {
var node = original.apply(null, arguments);
events.emit(node.type, node);
return node;
};
});
events.on('Identifier', function(node) { console.log(node); });
luaparse.parse('i = "foo"');
```
_this is only an example to illustrate what is possible and this particular
example might not suit your needs as the end location of the node has not been
determined yet. If you desire events you should use the `onCreateNode` callback
instead)._
### Lexer
The lexer used by luaparse can be used independently of the recursive descent
parser. The lex function is exposed as `luaparse.lex()` and it will return the
next token up until `EOF` is reached.
Each token consists of:
- `type` expressed as an enum flag which can be matched with `luaparse.tokenTypes`.
- `value`
- `line`, `lineStart`
- `range` can be used to slice out raw values, eg. `foo = "bar"` will return a
`StringLiteral` token with the value `bar`. Slicing out the range on the other
hand will return `"bar"`.
```js
var parser = luaparse.parse('foo = "bar"', { wait: true });
parser.lex(); // { type: 8, value: "foo", line: 1, lineStart: 0, range: [0, 3] }
parser.lex(); // { type: 32, value: "=", line: 1, lineStart: 0, range: [4, 5]}
parser.lex(); // { type: 2, value: "bar", line: 1, lineStart: 0, range: [6, 11] }
parser.lex(); // { type: 1, value: "<eof>", line: 1, lineStart: 0, range: [11 11] }
parser.lex(); // { type: 1, value: "<eof>", line: 1, lineStart: 0, range: [11 11] }
```
## Examples
Have a look in the [examples directory](https://github.com/oxyc/luaparse/tree/master/examples)
of the repository for some code examples or check them out [live](https://oxyc.github.io/luaparse/examples.html).
## luaparse(1)
The `luaparse` executable can be used in your shell by installing `luaparse` globally using npm:
```bash
$ npm install -g luaparse
$ luaparse --help
Usage: luaparse [option]... [file|code]...
Options:
-c|--code [code] parse code snippet
-f|--file [file] parse from file
-b|--beautify output an indenteted AST
--[no]-comments store comments. defaults to true
--[no]-scope store variable scope. defaults to false
--[no]-locations store location data on syntax nodes. defaults to false
--[no]-ranges store start and end character locations. defaults to false
-q|--quiet suppress output
-h|--help
-v|--version
--verbose
Examples:
luaparse --no-comments -c "locale foo = \"bar\""
luaparse foo.lua bar.lua
```
Example usage
```bash
$ luaparse "i = 0"
{"type":"Chunk","body":[{"type":"AssignmentStatement","variables":[{"type":"Identifier","name":"i"}],"init":[{"type":"NumericLiteral","value":0,"raw":"0"}]}],"comments":[]}
```
## Support
Has been tested in at least IE6+, Firefox 3+, Safari 4+, Chrome 10+, Opera 10+,
Node 0.4.0+, RingoJS 0.8-0.9, Narwhal 0.3.2, Rhino 1.7R4-1.7R5, Nashorn 1.8.0.
## Quality Assurance
_TL;DR simply run `make qa`. This will run all quality assurance scripts but
assumes you have it set up correctly._
Begin by cloning the repository and installing the development dependencies
with `npm install`. To test AMD loading for browsers you should run `bower
install` which will download RequireJS.
The luaparse test suite uses [testem](https://github.com/airportyh/testem) as a
test runner, and because of this it's very easy to run the tests using
different javascript engines or even on locally installed browsers. Currently
the default runner uses [PhantomJS](http://phantomjs.org/) and node so when
using `make test` or `npm test` you should have PhantomJS installed.
### Test runners
- `make test` uses PhantomJS and node.
- `make testem-engines` uses PhantomJS, node, narwhal, ringo, rhino and rhino
1.7R5. This requires that you have the engines installed.
- `make test-node` uses a custom command line reporter to make the output
easier on the eyes while practicing TDD.
- By installing `testem` globally you can also run the tests in a locally
installed browser.
### Other quality assurance measures
- You can check the function complexity using [complexity-report](https://github.com/philbooth/complexityReport.js)
using `make complexity-analysis`
- Running `make coverage` will generate the [coverage report](https://oxyc.github.io/luaparse/coverage.html).
To simply check that all code has coverage you can run `make coverage-analysis`.
- `make lint`, `make benchmark`, `make profile`.
### Documentation
By running `make docs` all [documentation](https://oxyc.github.io/luaparse/)
will be generated.
## Projects using luaparse
- [luamin](http://mths.be/luamin), a Lua minifier written by Mathias Bynens.
- [Ace](https://github.com/ajaxorg/ace), an online code editor.
## Acknowledgements
* Initial tests are scaffolded from [yueliang][yueliang] and then manually checked for error.
* Much of the code is based on [LuaMinify][luaminify], the [Lua][lua] source and [Esprima][esprima]. All awesome projects.
## License
MIT
[luaminify]: https://github.com/stravant/LuaMinify
[yueliang]: http://yueliang.luaforge.net/
[lua]: http://www.lua.org
[esprima]: http://esprima.org

View File

@@ -0,0 +1,120 @@
#!/usr/bin/env node
/*jshint node:true*/
/*globals console:true */
'use strict';
var fs = require('fs')
, util = require('util')
, luaparse = require('../luaparse')
, args = process.argv.splice(2)
, stdin = process.stdin
, isTTY = stdin.isTTY
, beautify = false
, quiet = false
, verbose = false
, input = ''
, options = {}
, snippets = [];
function usage() {
console.log([
"Usage: luaparse [option]... [file|code]..."
, "\nOptions:"
, " -c|--code [code] parse code snippet"
, " -f|--file [file] parse from file"
, " -b|--beautify output an indenteted AST"
, " --[no]-comments store comments. defaults to true"
, " --[no]-scope store variable scope. defaults to false"
, " --[no]-locations store location data on syntax nodes. defaults to false"
, " --[no]-ranges store start and end character locations. defaults to false"
, " -q|--quiet suppress output"
, " -h|--help"
, " -v|--version"
, " --verbose"
, "\nExamples:"
, " luaparse --no-comments -c \"locale foo = \\\"bar\\\"\""
, " luaparse foo.lua bar.lua"
].join("\n"));
}
if (isTTY && !args.length) {
usage();
process.exit(0);
}
for (var i = 0, l = args.length; i < l; i++) {
var arg = args[i], match, flag, bool, snippet;
if (match = /^(?:-|--)(?:(no)-)?(\w+)$/.exec(arg)) {
bool = 'no' !== match[1];
flag = match[2];
switch (flag) {
case 'b': case 'beautify':
beautify = true;
continue;
case 'q': case 'quiet':
quiet = true;
continue;
case 'verbose':
verbose = true;
continue;
case 'c': case 'code':
snippets.push({ name: args[++i], code: args[i] });
continue;
case 'f': case 'file':
snippets.push({ name: args[++i], code: fs.readFileSync(args[i], 'utf-8') });
continue;
case 'comments': case 'scope': case 'locations': case 'ranges':
options[flag] = bool;
continue;
case 'v': case 'version':
console.log("luaparse v%s", luaparse.version);
process.exit(0);
break;
case 'h': case 'help':
usage();
process.exit(0);
break;
default:
if (!quiet) console.log("Unknown option: %s", match[0]);
process.exit(2);
}
}
// Default to autodetecting code or file.
snippet = fs.existsSync(arg) ?
{ name: arg, code: fs.readFileSync(arg, 'utf-8') } :
{ name: arg, code: arg };
snippets.push(snippet);
}
function done() {
var success = true;
snippets.forEach(function(snippet) {
var message, ast;
try {
ast = luaparse.parse(snippet.code, options);
message = beautify ? JSON.stringify(ast, null, ' ')
: JSON.stringify(ast);
} catch(e) {
message = util.format("%s: %s", snippet.name, e.message);
if (verbose) console.log(e.stack);
success = false;
}
if (!quiet) console.log(message);
});
if (!success) process.exit(1);
}
if (stdin.isTTY) done();
else {
// @TODO doesn't work in node 0.9.4-0.9.11 because `end` is never emitted.
stdin.setEncoding('utf8');
stdin.on('data', function(chunk) { input += chunk; });
stdin.on('end', function() { snippets.unshift(input.trim()); done(); });
stdin.resume();
}
/* vim: set sw=2 ts=2 et tw=80 ft=javascript : */

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,21 @@
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*)
if command -v cygpath > /dev/null 2>&1; then
basedir=`cygpath -w "$basedir"`
fi
;;
esac
if [ -z "$NODE_PATH" ]; then
export NODE_PATH="/home/allen/projects/computercraft/computercraft-ts/node_modules/.pnpm/luaparse@0.2.1/node_modules/luaparse/bin/node_modules:/home/allen/projects/computercraft/computercraft-ts/node_modules/.pnpm/luaparse@0.2.1/node_modules/luaparse/node_modules:/home/allen/projects/computercraft/computercraft-ts/node_modules/.pnpm/luaparse@0.2.1/node_modules:/home/allen/projects/computercraft/computercraft-ts/node_modules/.pnpm/node_modules"
else
export NODE_PATH="/home/allen/projects/computercraft/computercraft-ts/node_modules/.pnpm/luaparse@0.2.1/node_modules/luaparse/bin/node_modules:/home/allen/projects/computercraft/computercraft-ts/node_modules/.pnpm/luaparse@0.2.1/node_modules/luaparse/node_modules:/home/allen/projects/computercraft/computercraft-ts/node_modules/.pnpm/luaparse@0.2.1/node_modules:/home/allen/projects/computercraft/computercraft-ts/node_modules/.pnpm/node_modules:$NODE_PATH"
fi
if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/../../bin/luaparse" "$@"
else
exec node "$basedir/../../bin/luaparse" "$@"
fi

View File

@@ -0,0 +1,48 @@
{
"name": "luaparse",
"version": "0.2.1",
"description": "A Lua parser in JavaScript",
"keywords": [
"ast",
"lua",
"parser",
"parsing"
],
"homepage": "https://oxyc.github.io/luaparse/",
"bugs": "https://github.com/oxyc/luaparse/issues",
"license": "MIT",
"author": "Oskar Schöldström <public@oxy.fi> (http://www.oxy.fi/)",
"files": [
"README.md",
"LICENSE",
"luaparse.js",
"bin/luaparse"
],
"main": "luaparse.js",
"bin": "./bin/luaparse",
"man": "./docs/luaparse.1",
"repository": {
"type": "git",
"url": "https://github.com/oxyc/luaparse.git"
},
"scripts": {
"test": "make qa"
},
"devDependencies": {
"benchmark": "~1.0.0",
"complexity-report": "~0.10.5",
"docco": "~0.6.3",
"gulp": "~3.8.10",
"gulp-add-src": "^0.2.0",
"gulp-filelog": "^0.4.1",
"gulp-header": "^1.2.2",
"gulp-jshint": "^1.9.0",
"gulp-rename": "^1.2.0",
"gulp-striphtml": "0.0.1",
"gulp-uglify": "^1.0.1",
"istanbul": "~0.3.2",
"marked": "~0.3.2",
"spec": "git://github.com/kitcambridge/spec.git#gh-pages",
"testem": "~0.6.9"
}
}