maths22 / sexp-php
dev-master
2019-08-27 01:05 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-20 19:35:10 UTC
README
一个用于处理S表达式(符号表达式)的简单类。来自维基百科
S表达式或sexps(代表“符号表达式”)是基于列表的数据结构,用于表示半结构化数据。S表达式可以是一个嵌套更小S表达式的列表。S表达式最著名的用途是在Lisp系列编程语言中。
请注意,这个解析器不适用于处理非常大的文件(数兆字节以上),因为整个文件被解析并存储在一个基于数组的内存结构中。
支持的语法
此实现支持以下语法(BNF格式)
<sexpr> :: <string> | <list>
<string> :: <token> | <base64> | <hex> | <quoted> ;
<token> :: <tokenchar>+ ;
<base64> :: "|" ( <alpha> | <digit> | "+" | "/" | "=" )* "|" ;
<hex> :: "#" ( <digit> | "A" ... "F" | "a" ... "f" | <space> )* "#" ;
<quoted> :: "\"" <bytes> "\"" | "'" <bytes> "'" ;
<list> :: "(" ( <sexp> | <space> )* ")" ;
<tokenchar> :: <alpha> | <digit> | <punc> ;
<space> :: ( " " | "\t" | "\r" | "\n" )* ;
<digit> :: "0" ... "9" ;
<alpha> :: "A" ... "Z" | "a" ... "z" | <digit> ;
<punc> :: "-" | "." | "/" | "_" | ":" | "*" | "+" | "=" ;
<comment> :: ";" <bytes> -- until the end of line
示例s表达式语法
(foo
"this is a \"quoted\" string"
"also supporting\nnew lines"
"The following number will be automatically casted to an integer"
1231
"unless you call ->setCastNumbers(false)"
(bar
; Line comment
(int 10) ; This is a comment too
(float 3.1415)
(string "foobar")
(string 'single quotes')
(base64 |YWJj|) ; "abc" encoded in base64
(hex #61 62 63#) ; "abc" encoded as hex octets
)
)
库使用
$sexp = new \DrSlump\Sexp();
// Parse an s-expression and return an array with the data
$arr = $sexp->parse('(foo (bar 10) baz)');
print_r($arr);
// Array
// (
// [0] => foo
// [1] => Array
// (
// [0] => bar
// [1] => 10
// )
//
// [2] => baz
// )
// Serialize an array structure into an s-expression string
$exp = $sexp->serialize($arr);
许可证
The MIT License
Copyright (c) 2011 Iván -DrSlump- Montes
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.