Add some comments

This commit is contained in:
Alexander Luzgarev 2018-10-07 11:40:12 +02:00
parent 468d585d08
commit e15dcf572e
3 changed files with 163 additions and 5 deletions

View File

@ -153,11 +153,11 @@ namespace ConsoleDemo
public static void Main(string[] args) public static void Main(string[] args)
{ {
ParserDemo(); //ParserDemo();
//SemanticsDemo(); //SemanticsDemo();
//ContextDemo(); //ContextDemo();
//DumbPrinterDemo(); //DumbPrinterDemo();
//UsageDemo(); UsageDemo();
Console.ReadKey(); Console.ReadKey();
} }
} }

View File

@ -19,7 +19,7 @@ namespace Parser.Internal
public GreenNode(TokenKind kind, int slots) public GreenNode(TokenKind kind, int slots)
{ {
Kind =kind; Kind = kind;
Slots = slots; Slots = slots;
} }

View File

@ -1,115 +1,273 @@
namespace Parser namespace Parser
{ {
// We use the same set of kinds for syntax tokens & syntax nodes.
public enum TokenKind public enum TokenKind
{ {
// SYNTAX TOKENS
None = 0, None = 0,
// The lexer puts a virtual "end of file" token at the end of the parsed file.
EndOfFile = 1, EndOfFile = 1,
// Identifier: could be a reserved word, a variable name, a class name, etc.
Identifier = 2, Identifier = 2,
// Number literal: 123, 45.678, 2e-5, etc.
NumberLiteral = 3, NumberLiteral = 3,
// String literal: 'abc', '123', etc. The "usual" string literals are single-quoted and are just char arrays.
StringLiteral = 4, StringLiteral = 4,
// Double-quoted string literal: "abc", "123", etc. These are the "new" string literal that are more like strings
// and less like char arrays (for example, char arrays could be columns instead of rows, or even multi-dimensional).
DoubleQuotedStringLiteral = 5, DoubleQuotedStringLiteral = 5,
// This is for supporting "command statements" like
// > cd some/+folder/
// In this example, "some/folder" should be treated as a string literal (for example, "+' there should be a part
// of it, and not parsed as a binary operator).
UnquotedStringLiteral = 6, UnquotedStringLiteral = 6,
// trivia // trivia
// Spaces, tabs, etc.
Whitespace = 10, Whitespace = 10,
// New line characters.
Newline = 11, Newline = 11,
// There are three types of comments:
// * comments starting with % and lasting till the end of the line;
// * comments starting with %{ and ending with %}, possibly spanning several lines;
// * comments starting with ... and lasting till the end of the line.
// At the moment, this token is used to accomodate all of them, so the other two
// (MultilineComment = 13 and DotDotDot = 56 are not used).
Comment = 12, Comment = 12,
// Comments starting with %{ and ending with %}, possibly spanning several lines. Not used, see Comment = 12.
MultilineComment = 13, MultilineComment = 13,
// operators
// =
Assignment = 20, Assignment = 20,
// ==
Equality = 21, Equality = 21,
// ~=
Inequality = 22, Inequality = 22,
// &&
LogicalAnd = 23, LogicalAnd = 23,
// ||
LogicalOr = 24, LogicalOr = 24,
// &
BitwiseAnd = 25, BitwiseAnd = 25,
// |
BitwiseOr = 26, BitwiseOr = 26,
// <
Less = 27, Less = 27,
// <=
LessOrEqual = 28, LessOrEqual = 28,
// >
Greater = 29, Greater = 29,
// >=
GreaterOrEqual = 30, GreaterOrEqual = 30,
// ~
Not = 31, Not = 31,
// +
Plus = 32, Plus = 32,
// -
Minus = 33, Minus = 33,
// *
Multiply = 34, Multiply = 34,
// /
Divide = 35, Divide = 35,
// ^
Power = 36, Power = 36,
// \
Backslash = 37, Backslash = 37,
// ' (this is the same as starting string literal; we'll have some fun distinguishing those).
Transpose = 38, Transpose = 38,
// .*
DotMultiply = 39, DotMultiply = 39,
// ./
DotDivide = 40, DotDivide = 40,
// .^
DotPower = 41, DotPower = 41,
// .\
DotBackslash = 42, DotBackslash = 42,
// .'
DotTranspose = 43, DotTranspose = 43,
// @
At = 44, At = 44,
// :
Colon = 45, Colon = 45,
// ?
QuestionMark = 46, QuestionMark = 46,
// ,
Comma = 47, Comma = 47,
// ;
Semicolon = 48, Semicolon = 48,
// {
OpeningBrace = 49, OpeningBrace = 49,
// }
ClosingBrace = 50, ClosingBrace = 50,
// [
OpeningSquareBracket = 51, OpeningSquareBracket = 51,
// ]
ClosingSquareBracket = 52, ClosingSquareBracket = 52,
// (
OpeningBracket = 53, OpeningBracket = 53,
// )
ClosingBracket = 54, ClosingBracket = 54,
// .
Dot = 55, Dot = 55,
// Comments starting with ... and lasting till the end of the line. Not used, see Comment = 12.
DotDotDot = 56, DotDotDot = 56,
// unary tokens are not recognized during lexing; they are contextually recognized while parsing. // Unary tokens are not recognized during lexing; they are contextually recognized while parsing.
UnaryPlus = 57, UnaryPlus = 57,
UnaryMinus = 58, UnaryMinus = 58,
UnaryNot = 59, UnaryNot = 59,
UnaryQuestionMark = 60, UnaryQuestionMark = 60,
// syntax nodes
// SYNTAX NODES
// The whole file.
File = 100, File = 100,
// a list of syntax nodes and/or tokens.
List, List,
// [output1, output2] = function(input1, input2)
// <...>
// end
FunctionDeclaration, FunctionDeclaration,
// (input1, input2)
FunctionInputDescription, FunctionInputDescription,
// [output1, output2] =
FunctionOutputDescription, FunctionOutputDescription,
// switch a
// case 1
// <...>
// end
SwitchStatement, SwitchStatement,
// case 1
// doSomething();
SwitchCase, SwitchCase,
// while a < 10
// doSomething();
// end
WhileStatement, WhileStatement,
// if a < 5
// doSomething();
// elseif a > 10
// doSomethingElse();
// else
// GiveUp();
// end
IfStatement, IfStatement,
// elseif a > 10
// doSomethingElse();
ElseifClause, ElseifClause,
// else
// GiveUp();
ElseClause, ElseClause,
// for a = 1:5
// process(a);
// end
ForStatement, ForStatement,
// a = 1:5
AssignmentExpression, AssignmentExpression,
// catch e
// dealWithIt(e);
// end
CatchClause, CatchClause,
// try
// somethingWeird();
// catch e
// dealWithIt(e);
// end
TryCatchStatement, TryCatchStatement,
// a = 5;
ExpressionStatement, ExpressionStatement,
//
EmptyStatement, EmptyStatement,
//
EmptyExpression, EmptyExpression,
// -13
UnaryPrefixOperationExpression, UnaryPrefixOperationExpression,
// some.complex.name
CompoundName, CompoundName,
// @func
NamedFunctionHandle, NamedFunctionHandle,
// @(x) x + 1
Lambda, Lambda,
// +
BinaryOperation, BinaryOperation,
// a
IdentifierName, IdentifierName,
// 123
NumberLiteralExpression, NumberLiteralExpression,
// 'abc'
StringLiteralExpression, StringLiteralExpression,
// "abc"
DoubleQuotedStringLiteralExpression, DoubleQuotedStringLiteralExpression,
// abc
UnquotedStringLiteralExpression, UnquotedStringLiteralExpression,
// [1, 2; 3 4]
ArrayLiteralExpression, ArrayLiteralExpression,
// {1, 3, 'abc'}
CellArrayLiteralExpression, CellArrayLiteralExpression,
// (1 + 2 * 3)
ParenthesizedExpression, ParenthesizedExpression,
// abc{2}
CellArrayElementAccess, CellArrayElementAccess,
// doSomething(5)
FunctionCall, FunctionCall,
// object.member
MemberAccess, MemberAccess,
// [1 2 3]'
UnaryPostfixOperationExpression, UnaryPostfixOperationExpression,
// struct.(field)
IndirectMemberAccess, IndirectMemberAccess,
// cd some/+folder/
Command, Command,
// method@SuperClass(object)
ClassInvokation, ClassInvokation,
// = true
AttributeAssignment, AttributeAssignment,
// Sealed = true
Attribute, Attribute,
// (Sealed = true)
AttributeList, AttributeList,
// function result = method(obj)
// <...>
// end
MethodDefinition, MethodDefinition,
// methods
// function result = method(obj)
// <...>
// end
// end
MethodsList, MethodsList,
// properties
// x
// y
// end
PropertiesList, PropertiesList,
// < BaseClass, AnotherBaseClass
BaseClassList, BaseClassList,
// classdef MyClass < BaseClass, AnotherBaseClass
// properties
// y
// end
// methods
// <...>
// end
// end
ClassDeclaration, ClassDeclaration,
// (1)
EnumerationItemValue, EnumerationItemValue,
// One (1)
EnumerationItem, EnumerationItem,
// enumeration
// One (1)
// Two (2)
// end
EnumerationList, EnumerationList,
// result = abstractMethod(object)
AbstractMethodDeclaration, AbstractMethodDeclaration,
// events
// ToggleSomething
// end
EventsList, EventsList,
} }
} }