Magpie语法解析器
Magpie是一个用go
语言写的语法解析器,支持class, linq, sql, net, http, fmt, json等。还包含一个实时语法高亮的REPL。
例子
class Linq {
static fn TestSimpleLinq() {
//Prepare Data Source
let ingredients = [
{Name: "Sugar", Calories: 500},
{Name: "Egg", Calories: 100},
{Name: "Milk", Calories: 150},
{Name: "Flour", Calories: 50},
{Name: "Butter", Calories: 200},
]
//Query Data Source
ingredient = from i in ingredients where i.Calories >= 150 orderby i.Name select i
//Display
for item in ingredient => println(item)
}
static fn TestFileLinq() {
//Read Data Source from file.
file = newFile("./examples/linqSample.csv", "r")
//Query Data Source
result = from field in file where int(field[1]) > 300000 select field[0]
//Display
for item in result => printf("item = %s\n", item)
file.close()
}
static fn TestComplexLinq() {
//Data Source
stringList = [
"A penny saved is a penny earned.",
"The early bird catches the worm.",
"The pen is mightier than the sword."
]
//Query Data Source
earlyBirdQuery =
from sentence in stringList
let words = sentence.split(" ")
from word in words
let w = word.lower()
where w[0] == "a" || w[0] == "e" ||
w[0] == "i" || w[0] == "o" ||
w[0] == "u"
select word
//Display
for v in earlyBirdQuery => printf("'%s' starts with a vowel\n", v)
}
}
Linq.TestSimpleLinq()
println("======================================")
Linq.TestFileLinq()
println("======================================")
Linq.TestComplexLinq()
