define language rules
yorkie opened this issue · 4 comments
yorkie commented
Quip still use similar token rules to Javascript, so large number of rules is javascript's rules. All rules is defined in https://github.com/yorkie/quip/blob/master/src/token.h#L17-L77
This issue would be a documentation to specify the meaning of every tokens:
- Separator:
ILLEGAL
: shows that an invalided syntax, this might be an error in runtime, for example, a special character like^
maybe illegal.EOS
: is short forEnd of Source
, Yes, it means the program has eat up the source file, and related files.NEWLINE
: in literal, it means that a new line.ID
: Identifier of a token, this property must attach any token.
- Token: these characters in below should be in source codes, and has a meaning for your programs:
VAR
: like javascriptvar
, means start to a new variable.FUNCTION
: like javascriptfunction
.WHILE
: like javascript and most c-style languageswhile
.IF
: like javascript and most c-style languagesif
.ELSE
: like javascript and most c-style languageselse
.FOR
: like javascript and most c-style languagesfor
to make an iterator.RETURN
: like javascript and most c-style languagesreturn
to return a function.SETTER
:set
the field(function, value) of prototype.GETTER
:get
the field(function, value) of prototype.THIS
:this
, points to current object itself.SUPER
:super
, points to super class.NEW
:new
a function like javascript.INHERITS
:inherits
a base class(prototype).YIELD
: in ECMAScript 6, the keywordyield
is to resolve callback hell, in quip, is one stuff.TRY
: exception related token,try
.CATCH
: exception related token,catch
.THROW
: exception related token,throw
.FINALLY
: exception related token,finally
.LBRACE
: left brace, just a simple token{
.RBRACE
: right brace, just a simple token}
.LPAREN
: left paren, just a simple token(
.RPAREN
: right paren, just a simple token)
.LBRACKET
: left bracket, just a simple token[
.RBRACKET
: right bracket, just a simple token]
.COLON
: colon, just a simple token:
.QMARK
: question mark, just a simple token?
.SEMICOLON
: semicolon, just a simple token;
.COMMA
: comma, just a simple token,
.
- Operators:
OP_DOT
:.
.OP_NOT
:!
.OP_PLUS
:+
.OP_MINUS
:-
.OP_MUL
:*
.OP_DIV
:/
.OP_MOD
:%
.OP_GT
:>
.OP_LT
:<
.OP_GTE
:>=
.OP_LTE
:<=
.OP_EQ
:==
.OP_NE
:!=
.OP_AND
:&&
.OP_OR
:||
.OP_ASSIGN
:=
.OP_PLUS_ASSIGN
:+=
.OP_MINUS_ASSIGN
:-=
.OP_MUL_ASSIGN
:*=
.OP_DIV_ASSIGN
:/=
.OP_BIT_AND
:&
.OP_BIT_OR
:|
.OP_BIT_XOR
:^
.OP_BIT_NOT
:~
.OP_BIT_SHL
:<<
.OP_BIT_SHR
:>>
.
- Keywords:
var
function
while
if
else
for
let
set
, set the field(function, value) of prototypeget
, get the field(function, value) of prototypethis
super
new
inherits
yield
try
catch
finally
throw
return
yorkie commented
OPP Example:
// base class
function Base() {
this.foo = 10;
}
set Base.hellostr = "base";
set Base.hello = function() {
console.log(this.hellostr);
}
// sub class
function Sub() inherits Base {
this.bar = 20;
}
set Sub.hellostr = "sub class";
// runtime
var sub = new Sub();
sub.hello(); // print `sub class`
sub.foo; // 10
sub.bar; // 20
yorkie commented
Callback Example:
var fs = require('fs');
var net = require('net');
var d1, d2, d3;
// series
yield net.connect(80, 'www.google.com', function(err, socket) {
d1 = null;
d2 = null;
d3 = null;
// start progress
[err, d1] = yield fs.read('demo1.xml')
[err, d2] = yield fs.read('demo2.xml')
[err, d3] = yield fs.read('demo3.xml')
if (err) {
onError(err);
}
})
console.log(d1);
console.log(d2);
console.log(d3);
// parallel
yield net.connect(80, 'www.google.com', function(err, socket) {
d1 = null;
d2 = null;
d3 = null;
// start progress
(yield [
fs.read('demo1.xml'),
fs.read('demo2.xml'),
fs.read('demo3.xml')
]).foreach(function(err, data, index) {
if (err) return onError(err);
switch (index) {
case 0: d1 = data; break;
case 1: d2 = data; break;
case 2: d3 = data; break;
};
});
})
console.log(d1);
console.log(d2);
console.log(d3);
function onError(e, socket) {
throw e;
}
yorkie commented
http-req.q:
var net = require('net');
function Request(port, host) inherits net.Socket {
this.port = port;
this.host = host;
}
set Request {
port: 80,
host: 'localhost'
}
set Request.onData = function(err, chunk) {
// then we could parser data in this
}
set Request.onEnd = function() {
// then this socket is closed
}
set Request.request = function() {
return this.connect();
}
module.exports = Request;
use-http-req.q
var HTTPRequest = require('http-req');
var req = new HTTPRequest();
var res = yield req.request();
yorkie commented
This project has been redesigned few weeks ago, such that it doesn't create any new syntax out of ES standard no longer, then this documentation had been deprecated.