Nccc 0.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Nccc --version 0.3.0
NuGet\Install-Package Nccc -Version 0.3.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nccc" Version="0.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Nccc --version 0.3.0
#r "nuget: Nccc, 0.3.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Nccc as a Cake Addin
#addin nuget:?package=Nccc&version=0.3.0

// Install Nccc as a Cake Tool
#tool nuget:?package=Nccc&version=0.3.0

Nccc

.Net Compiler Compiler Combinator

Example

一个算术表达式的例子:

:: add

@set-delims '(' ')' '[' ']' '{' '}' '+' '-' '*' '/' '^'

add = (@or add:(mul '+' add)
           sub:(mul'-' add)
           mul)

mul = (@or mul:(pow '*' mul)
           div:(pow '/' mul)
           pow)

pow = (@or pow:(primary '^' pow)
           primary)

primary = (@or par:('(' add ')')
               par:('[' add ']')
               par:('{' add '}')
               neg:('-' pow)
               num:<number>)

加载Parser:

private NcParser _parser = NcParser.LoadFromAssembly(Assembly.GetExecutingAssembly(), "Nccc.Tests.Calculator.calculator.grammer");

Parse:

var pr = _parser.ScanAndParse("(5.1+2)*3+-2^3^2");
Console.WriteLine(pr.ToSExp().ToPrettyString());

输出Parsing结果:

(((parser add))
 (success? True)
 (nodes
  ((add[(1,1)-(1,17)]
    (mul[(1,1)-(1,10)]
     (par[(1,1)-(1,8)]
      (add[(1,2)-(1,7)]
       (num[(1,2)-(1,5)] 5.1)
       (num[(1,6)-(1,7)] 2)))
     (num[(1,9)-(1,10)] 3))
    (neg[(1,11)-(1,17)]
     (pow[(1,12)-(1,17)]
      (num[(1,12)-(1,13)] 2)
      (pow[(1,14)-(1,17)]
       (num[(1,14)-(1,15)] 3)
       (num[(1,16)-(1,17)] 2)))))))

Result Type

TODO

Grammer Overview

Grammer文件大部分采用了S表达式的语法。

Scanner

词法分析参数(TODO):

@set-delims '(' ')' '[' ']' '{' '}' '<' '>' '`' '::' '=' ':' '~' '??'
@set-line-comment ';'
@set-comment-start '#|'
@set-comment-end '|#'
@set-operators
@set-quotation-marks '\''
@set-regex-marks '/'
@set-lisp-char
@set-significant-whitespaces

@case-sensitive on
@split-word on

Parser

Options

@left-recur-detection on|off 左递归检测,会稍微影响性能,但是能排除

@use-memorized-parser on|off 中间结果缓存,能稍微改善Or操作的性能

Basic Parser

'string':匹配值为string的Token。若成功,返回空

<type>:匹配类型为type的Token

<*>:匹配所有Token

Variable

变量由大小写字母、数字、下划线和横杠组成,并且不能以数字开头。

variable = (@err'invalid var' /[a-zA-Z_\-][a-zA-Z0-9_\-]*/)

Combinator

(@.. p1 p2 p3 ...)或直接放括号里(p1 p2 p3 ...):按顺序parse

(@+ p1 p2 p3 ...):一个或多个

(@* p1 p2 p3 ...):一个或零个

(@,+ sep p1 p2 p3 ...):以sep隔开的token序列

(@,* sep p1 p2 p3 ...):匹配空或者以sep隔开的token序列

(@or p1 p2 p3 ...):匹配第一个成功parse的parser

(@! p1 p2 p3 ...):Not操作。若能被(p1 p2 p3 ...)成功parse,则失败;否则返回空

(@? p1 p2 p3 ...):匹配空或者(p1 p2 p3 ...)

~p:若能被p成功parse,则返回空

Named

name:parser:带名称的parser,匹配成功时增加一个type为name的节点

Error

(@err'string' p1 p2 p3 ...):匹配(p1 p2 p3 ...)。若失败,错误信息为string

Debug

调试用的操作符

??parser:打印parser的parse结果

[p1 p2 p3 ...]:打印(p1 p2 p3 ...)的parse结果

Travel AST

Node.Match

DigValue

DigNode

TODO

Product Compatible and additional computed target framework versions.
.NET Framework net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 616 11/28/2019
0.6.0 594 8/8/2019
0.5.0 564 8/8/2019
0.4.0 610 6/28/2019
0.3.0 701 12/29/2018

a parser parser