外观
syntax
原文: https://finproducts.atlassian.net/wiki/spaces/JIDD3/pages/34793521605/FIN+5.3.1+syntax 中文整理基于 FIN 5.3.1
导读: 编程语言的语法样式
以下为 API 参考,保留英文以便对照源码;目录已列出(英文标题便于检索);概述已尽量中译。
目录(导读)
- classes
- enums
- Slot Details
- close
- make
- writeDoc
- writeLine
- writeLines
- eachLine
- parse
- rules
- eachSegment
- num
- blockCommentEnd
- blockCommentStart
- blockCommentsNest
- brackets
- comments
- keywords
- loadForExt
- loadForFile
- strs
- delimiter
- delimiterEnd
- escape
- multiLine
- bracket
- comment
- fromStr
- keyword
- literal
- text
- vals
概述(中译)
类 HtmlSyntaxWriter : Obj
HtmlSyntaxWriter 将 SyntaxDoc 输出为 HTML
构造函数
方法
插槽详情
API 正文(英文)
close
Bool close()
Close underlying output stream
make
new make(OutStream out := Env.cur().out())
writeDoc
This writeDoc(SyntaxDoc doc)
Write an entire HTML file with proper head, body types using default CSS
writeLine
This writeLine(SyntaxLine line)
Write a single syntax line as styled HTML
writeLines
This writeLines(SyntaxDoc doc)
Write the lines of the document as HTML elements. This method does not generate HTML head/body tags.
SyntaxDoc
class SyntaxDoc : Obj
SyntaxDoc models a full document as a series of SyntaxLines.
Fields
| rules | Rules used to parse this document. |
Methods
| eachLine | Iterate each line of the document. |
| parse | Parse an input stream into a document using given rules. |
Slot Details
eachLine
Void eachLine(|SyntaxLine| f)
Iterate each line of the document.
parse
static SyntaxDoc parse(SyntaxRules rules, InStream in)
Parse an input stream into a document using given rules. The input stream is guaranteed to be closed.
rules
SyntaxRules rules { private set }
Rules used to parse this document.
SyntaxLine
class SyntaxLine : Obj
SyntaxLine models one parsed line of code
Fields
| num | One based line number |
Methods
| eachSegment | Iterate each segment span of text in the line |
Slot Details
eachSegment
Void eachSegment(|SyntaxType,Str| f)
Iterate each segment span of text in the line
num
const Int num
One based line number
SyntaxRules
@Js @Serializable const class SyntaxRules : Obj
SyntaxRules defines the syntax rules used to parse a specific programming language.
Constructors
| make | Default constructor with it-block |
Fields
| blockCommentEnd | End token for multi-line block comments |
| blockCommentStart | Start token for multi-line block comments |
| blockCommentsNest | Can block comments be nested (default is false). |
| brackets | Bracket characters defaults to "()[]{}". |
| comments | Start tokens for single line comments to end of line (list of strings). |
| keywords | List of the keywords. |
| strs | String and character literal styles |
Methods
| loadForExt | Load syntax rules for given file extension using "etc/syntax/ext.props". |
| loadForFile | Load syntax rules for given file. |
Slot Details
blockCommentEnd
const Str? blockCommentEnd
End token for multi-line block comments
blockCommentStart
const Str? blockCommentStart
Start token for multi-line block comments
blockCommentsNest
const Bool blockCommentsNest := false
Can block comments be nested (default is false).
brackets
const Str brackets := "(){}[]"
Bracket characters defaults to "()[]{}".
comments
const Str[]? comments
Start tokens for single line comments to end of line (list of strings).
keywords
const Str[]? keywords
List of the keywords.
loadForExt
static SyntaxRules? loadForExt(Str ext)
Load syntax rules for given file extension using "etc/syntax/ext.props". If no rules defined for extension return null.
loadForFile
static SyntaxRules? loadForFile(File file, Str? firstLine := null)
Load syntax rules for given file. If the file has already been parse then pass the first line to avoid re-reading the file to check the "#!" shebang. First we attempt to map the file extension to rules. If that fails, then we check the first line to see if defines a "#!" shebang. Return null if no rules can be determined.
make
new make(|This|? f := null)
Default constructor with it-block
strs
const SyntaxStr[]? strs
String and character literal styles
SyntaxStr
@Js @Serializable const class SyntaxStr : Obj
Syntax rules for a string or character literal
Fields
| delimiter | Token which delimits the start and end of the string. |
| delimiterEnd | Token which delimits the end of the string, or if null, then delimiter is assumed to be both the start and end of the string. |
| escape | Escape character placed before ending delimiter to indicate the delimiter is part of the string, not the end. |
| multiLine | Can this string literal span multiple lines |
Slot Details
delimiter
const Str delimiter := "\""
Token which delimits the start and end of the string. If the end delimiter is different, then also set the delimiterEnd field.
delimiterEnd
const Str? delimiterEnd
Token which delimits the end of the string, or if null, then delimiter is assumed to be both the start and end of the string.
escape
const Int escape
Escape character placed before ending delimiter to indicate the delimiter is part of the string, not the end. The escape character is also assumed to escape itself.
multiLine
const Bool multiLine := false
Can this string literal span multiple lines
SyntaxType
@Serializable { simple=true } enum class SyntaxType : Enum
SyntaxType models a syntax specific segment type such keyword or comment
Enum
| text | Normal text |
| bracket | Bracket such as {, }, (, ), [, or ] |
| keyword | Language specific keyword |
| literal | String literal |
| comment | Comment section either to end of line or multi-line block |
Constructors
| fromStr | Return the SyntaxType instance for the specified name. |
Fields
| vals | List of SyntaxType values indexed by ordinal |
Slot Details
bracket
const static SyntaxType bracket := ...
Bracket such as {, }, (, ), [, or ]
comment
const static SyntaxType comment := ...
Comment section either to end of line or multi-line block
fromStr
static new fromStr(Str name, Bool checked := true)
Return the SyntaxType instance for the specified name. If not a valid name and checked is false return null, otherwise throw ParseErr.
keyword
const static SyntaxType keyword := ...
Language specific keyword
literal
const static SyntaxType literal := ...
String literal
text
const static SyntaxType text := ...
Normal text
vals
const static SyntaxType[] vals := ...
List of SyntaxType values indexed by ordinal