raku.go 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. // raku
  2. /* Raku is an easy to use scripting language that can also be used easily interactively
  3. Syntax (verified LL(1) on smlweb.cpsc.ucalgary.ca)
  4. PROGRAM -> STATEMENTS.
  5. STATEMENTS -> STATEMENT STATEMENTS | .
  6. STATEMENT -> DEFINITION | EXPRESSION | BLOCK .
  7. DEFINITION -> to WORDS BLOCK.
  8. WORDS -> word WORDS | .
  9. EXPRESSION -> WORD_EXPRESSION | VALUE_EXPRESSION.
  10. WORD_EXPRESSION -> word WORD_CALLOP.
  11. WORD_CALLOP -> WORD_OPERATION | WORD_CALL.
  12. WORD_OPERATION -> operator PARAMETERS_NONEMPTY EOX.
  13. WORD_CALL -> PARAMETERS EOX.
  14. VALUE_EXPRESSION -> value VALUE_CALLOP.
  15. VALUE_CALLOP -> VALUE_OPERATION | VALUE_CALL.
  16. VALUE_OPERATION -> operator PARAMETERS_NONEMPTY EOX.
  17. VALUE_CALL -> EOX.
  18. PARAMETERS_NONEMPTY -> PARAMETER PARAMETERS.
  19. PARAMETERS -> PARAMETERS_NONEMPTY | .
  20. PARAMETER -> BLOCK | WORDVALUE .
  21. BLOCK -> ob STATEMENTS cb | op STATEMENTS cp | oa STATEMENTS ca | do
  22. STATEMENTS end.
  23. WORDVALUE -> word | VALUE.
  24. VALUE -> string | number | symbol.
  25. EOX -> eol | period.
  26. Lexer:
  27. */
  28. package raku
  29. import (
  30. "bytes"
  31. "fmt"
  32. "io"
  33. "reflect"
  34. "runtime"
  35. "strings"
  36. "unicode"
  37. "github.com/beoran/woe/graphviz"
  38. "github.com/beoran/woe/monolog"
  39. "github.com/beoran/woe/tree"
  40. )
  41. type Value string
  42. type TokenType int64
  43. type Position struct {
  44. Index int
  45. Row int
  46. Column int
  47. }
  48. const (
  49. TokenPeriod TokenType = TokenType('.')
  50. TokenComma TokenType = TokenType(',')
  51. TokenSemicolon TokenType = TokenType(';')
  52. TokenColon TokenType = TokenType(':')
  53. TokenOpenParen TokenType = TokenType('(')
  54. TokenCloseParen TokenType = TokenType(')')
  55. TokenOpenBrace TokenType = TokenType('{')
  56. TokenCloseBrace TokenType = TokenType('}')
  57. TokenOpenBracket TokenType = TokenType('[')
  58. TokenCloseBracket TokenType = TokenType(']')
  59. TokenNone TokenType = 0
  60. TokenError TokenType = -1
  61. TokenWord TokenType = -2
  62. TokenEOL TokenType = -3
  63. TokenEOF TokenType = -4
  64. TokenNumber TokenType = -5
  65. TokenOperator TokenType = -6
  66. TokenString TokenType = -7
  67. TokenSymbol TokenType = -8
  68. TokenFirstKeyword TokenType = -9
  69. TokenKeywordA TokenType = -10
  70. TokenKeywordDo TokenType = -11
  71. TokenKeywordEnd TokenType = -12
  72. TokenKeywordThe TokenType = -13
  73. TokenKeywordTo TokenType = -14
  74. TokenLastKeyword TokenType = -15
  75. TokenLast TokenType = -15
  76. )
  77. type Token struct {
  78. TokenType
  79. Value
  80. Position
  81. }
  82. var tokenTypeMap map[TokenType]string = map[TokenType]string{
  83. TokenNone: "TokenNone",
  84. TokenError: "TokenError",
  85. TokenWord: "TokenWord",
  86. TokenEOL: "TokenEOL",
  87. TokenEOF: "TokenEOF",
  88. TokenNumber: "TokenNumber",
  89. TokenOperator: "TokenOperator",
  90. TokenString: "TokenString",
  91. TokenSymbol: "TokenSymbol",
  92. TokenKeywordA: "TokenKeywordA",
  93. TokenKeywordDo: "TokenKeywordDo",
  94. TokenKeywordEnd: "TokenKeywordEnd",
  95. TokenKeywordThe: "TokenKeywordThe",
  96. TokenKeywordTo: "TokenKeywordTo",
  97. }
  98. var keywordMap map[string]TokenType = map[string]TokenType{
  99. "a": TokenKeywordA,
  100. "do": TokenKeywordDo,
  101. "end": TokenKeywordEnd,
  102. "the": TokenKeywordThe,
  103. "to": TokenKeywordTo,
  104. }
  105. var sigilMap map[string]TokenType = map[string]TokenType{
  106. "[": TokenOpenBracket,
  107. "{": TokenOpenBrace,
  108. "(": TokenOpenParen,
  109. "]": TokenCloseBracket,
  110. "}": TokenCloseBrace,
  111. ")": TokenCloseParen,
  112. }
  113. func (me TokenType) String() string {
  114. name, found := tokenTypeMap[me]
  115. if found {
  116. return name
  117. } else {
  118. if (me > 0) && (me < 256) {
  119. return fmt.Sprintf("TokenChar<%c>", byte(me))
  120. }
  121. return fmt.Sprintf("Unknown Token %d", int(me))
  122. }
  123. }
  124. func (me Token) String() string {
  125. return fmt.Sprintf("Token: %s >%s< %d %d %d.", me.TokenType, string(me.Value), me.Index, me.Row, me.Column)
  126. }
  127. type TokenChannel chan *Token
  128. type Lexer struct {
  129. Reader io.Reader
  130. Current Position
  131. Last Position
  132. Token Token
  133. rule LexerRule
  134. Output TokenChannel
  135. buffer []byte
  136. runes []rune
  137. }
  138. type LexerRule func(lexer *Lexer) LexerRule
  139. func (me *Lexer) Emit(t TokenType, v Value) {
  140. tok := &Token{t, v, me.Current}
  141. me.Output <- tok
  142. }
  143. func (me *Lexer) Error(message string, args ...interface{}) {
  144. value := fmt.Sprintf(message, args...)
  145. monolog.Error("Lex Error: %s", value)
  146. me.Emit(TokenError, Value(value))
  147. }
  148. func LexError(me *Lexer) LexerRule {
  149. me.Error("Error")
  150. return nil
  151. }
  152. func (me *Lexer) SkipComment() bool {
  153. if me.Peek() == '#' {
  154. if me.Next() == '(' {
  155. return me.SkipNotIn(")")
  156. } else {
  157. return me.SkipNotIn("\r\n")
  158. }
  159. }
  160. return true
  161. }
  162. /* Returns whether or not a keyword was found, and if so, the TokenType
  163. of the keyword.*/
  164. func LookupKeyword(word string) (bool, TokenType) {
  165. kind, found := keywordMap[word]
  166. return found, kind
  167. }
  168. /* Returns whether or not a special operator or sigil was found, and if so,
  169. returns the TokenTyp of the sigil.*/
  170. func LookupSigil(sigil string) (bool, TokenType) {
  171. fmt.Printf("LookupSigil: %s\n", sigil)
  172. kind, found := sigilMap[sigil]
  173. return found, kind
  174. }
  175. func LexWord(me *Lexer) LexerRule {
  176. me.SkipNotIn(" \t\r\n'")
  177. iskw, kind := LookupKeyword(me.CurrentStringValue())
  178. if iskw {
  179. me.Found(kind)
  180. } else {
  181. me.Found(TokenWord)
  182. }
  183. return LexNormal
  184. }
  185. func LexSymbol(me *Lexer) LexerRule {
  186. me.SkipNotIn(" \t\r\n'")
  187. me.Found(TokenSymbol)
  188. return LexNormal
  189. }
  190. func LexNumber(me *Lexer) LexerRule {
  191. me.SkipNotIn(" \tBBBT\r\n")
  192. me.Found(TokenNumber)
  193. return LexNormal
  194. }
  195. func LexWhitespace(me *Lexer) LexerRule {
  196. me.SkipWhitespace()
  197. me.Advance()
  198. return LexNormal
  199. }
  200. func LexComment(me *Lexer) LexerRule {
  201. if !me.SkipComment() {
  202. me.Error("Unterminated comment")
  203. return LexError
  204. }
  205. me.Advance()
  206. return LexNormal
  207. }
  208. func LexPunctuator(me *Lexer) LexerRule {
  209. me.Found(TokenType(me.Peek()))
  210. return LexNormal
  211. }
  212. func LexEOL(me *Lexer) LexerRule {
  213. me.SkipIn("\r\n")
  214. me.Found(TokenEOL)
  215. return LexNormal
  216. }
  217. func LexOperator(me *Lexer) LexerRule {
  218. me.SkipNotIn(" \t\r\n")
  219. issig, kind := LookupSigil(me.CurrentStringValue())
  220. if issig {
  221. me.Found(kind)
  222. } else {
  223. me.Found(TokenOperator)
  224. }
  225. return LexNormal
  226. }
  227. func lexEscape(me *Lexer) error {
  228. _ = me.Next()
  229. return nil
  230. }
  231. func LexString(me *Lexer) LexerRule {
  232. open := me.Peek()
  233. do_escape := open == '"'
  234. peek := me.Next()
  235. me.Advance()
  236. for ; peek != '\000'; peek = me.Next() {
  237. if do_escape && peek == '\\' {
  238. if err := lexEscape(me); err != nil {
  239. return LexError
  240. }
  241. } else if peek == open {
  242. me.Found(TokenString)
  243. _ = me.Next()
  244. me.Advance()
  245. return LexNormal
  246. }
  247. }
  248. me.Error("Unexpected EOF in string.")
  249. return nil
  250. }
  251. func LexNumberOrOperator(me *Lexer) LexerRule {
  252. if unicode.IsDigit(me.Next()) {
  253. return LexNumber
  254. } else {
  255. _ = me.Previous()
  256. return LexOperator
  257. }
  258. }
  259. func LexNormal(me *Lexer) LexerRule {
  260. peek := me.Peek()
  261. if peek == '#' {
  262. return LexComment
  263. } else if strings.ContainsRune(" \t", peek) {
  264. return LexWhitespace
  265. } else if strings.ContainsRune(".,;:", peek) {
  266. return LexPunctuator
  267. } else if strings.ContainsRune("$", peek) {
  268. return LexSymbol
  269. } else if strings.ContainsRune("\r\n", peek) {
  270. return LexEOL
  271. } else if strings.ContainsRune("+-", peek) {
  272. return LexNumberOrOperator
  273. } else if strings.ContainsRune("\"`", peek) {
  274. return LexString
  275. } else if peek == '\000' {
  276. me.Emit(TokenEOF, "")
  277. return nil
  278. } else if unicode.IsLetter(peek) {
  279. return LexWord
  280. } else if unicode.IsDigit(peek) {
  281. return LexNumber
  282. } else {
  283. return LexOperator
  284. }
  285. }
  286. func OpenLexer(reader io.Reader) *Lexer {
  287. lexer := &Lexer{}
  288. lexer.Reader = reader
  289. lexer.Output = make(TokenChannel)
  290. // lexer.buffer = new(byte[1024])
  291. return lexer
  292. }
  293. func (me *Lexer) ReadReaderOnce() (bool, error) {
  294. buffer := make([]byte, 1024)
  295. n, err := me.Reader.Read(buffer)
  296. monolog.Debug("read %v %d %v\n", buffer[:n], n, err)
  297. if n > 0 {
  298. me.buffer = append(me.buffer, buffer[:n]...)
  299. monolog.Debug("append %s", me.buffer)
  300. }
  301. if err == io.EOF {
  302. return true, nil
  303. } else if err != nil {
  304. me.Error("Error reading from reader: %s", err)
  305. return true, err
  306. }
  307. return false, nil
  308. }
  309. func (me *Lexer) ReadReader() error {
  310. me.buffer = make([]byte, 0)
  311. more, err := me.ReadReaderOnce()
  312. for err == nil && more {
  313. more, err = me.ReadReaderOnce()
  314. }
  315. me.runes = bytes.Runes(me.buffer)
  316. return err
  317. }
  318. func (me *Lexer) Peek() rune {
  319. if (me.Current.Index) >= len(me.runes) {
  320. return '\000'
  321. }
  322. return me.runes[me.Current.Index]
  323. }
  324. func (me *Lexer) PeekNext() rune {
  325. if (me.Current.Index + 1) >= len(me.runes) {
  326. return '\000'
  327. }
  328. return me.runes[me.Current.Index+1]
  329. }
  330. func (me *Lexer) Next() rune {
  331. if me.Peek() == '\n' {
  332. me.Current.Column = 0
  333. me.Current.Row++
  334. }
  335. me.Current.Index++
  336. if me.Current.Index >= len(me.runes) {
  337. //me.Emit(TokenEOF, "")
  338. }
  339. return me.Peek()
  340. }
  341. func (me *Lexer) Previous() rune {
  342. if me.Current.Index > 0 {
  343. me.Current.Index--
  344. if me.Peek() == '\n' {
  345. me.Current.Column = 0
  346. me.Current.Row++
  347. }
  348. }
  349. return me.Peek()
  350. }
  351. func (me *Lexer) SkipRune() {
  352. _ = me.Next()
  353. }
  354. func (me *Lexer) SkipIn(set string) bool {
  355. for strings.ContainsRune(set, me.Next()) {
  356. monolog.Debug("SkipIn: %s %c\n", set, me.Peek())
  357. if me.Peek() == '\000' {
  358. return false
  359. }
  360. }
  361. return true
  362. }
  363. func (me *Lexer) SkipNotIn(set string) bool {
  364. _ = me.Next()
  365. for !strings.ContainsRune(set, me.Peek()) {
  366. if me.Next() == '\000' {
  367. return false
  368. }
  369. }
  370. return true
  371. }
  372. func (me *Lexer) SkipWhile(should_skip func(r rune) bool) bool {
  373. for should_skip(me.Peek()) {
  374. if me.Next() == '\000' {
  375. return false
  376. }
  377. }
  378. return true
  379. }
  380. func (me *Lexer) SkipWhitespace() {
  381. me.SkipIn(" \t")
  382. }
  383. func (me *Lexer) Advance() {
  384. me.Last = me.Current
  385. }
  386. func (me *Lexer) Rewind() {
  387. me.Current = me.Last
  388. }
  389. func (me *Lexer) CurrentRuneValue() []rune {
  390. return me.runes[me.Last.Index:me.Current.Index]
  391. }
  392. func (me *Lexer) CurrentStringValue() string {
  393. return string(me.CurrentRuneValue())
  394. }
  395. func (me *Lexer) Found(kind TokenType) {
  396. me.Emit(kind, Value(me.CurrentStringValue()))
  397. me.Advance()
  398. }
  399. func GetFunctionName(fun interface{}) string {
  400. return runtime.FuncForPC(reflect.ValueOf(fun).Pointer()).Name()
  401. }
  402. func (me *Lexer) Start() {
  403. if err := me.ReadReader(); err == nil || err == io.EOF {
  404. rule := LexNormal
  405. for rule != nil {
  406. monolog.Debug("Lexer Rule: %s\n", GetFunctionName(rule))
  407. rule = rule(me)
  408. }
  409. } else {
  410. me.Error("Could not read in input buffer: %s", err)
  411. }
  412. close(me.Output)
  413. }
  414. func (me *Lexer) TryLexing() {
  415. go me.Start()
  416. for token := range me.Output {
  417. monolog.Info("Token %s", token)
  418. }
  419. }
  420. type AstType int
  421. const (
  422. AstTypeProgram = AstType(iota)
  423. AstTypeStatements
  424. AstTypeStatement
  425. AstTypeDefinition
  426. AstTypeWords
  427. AstTypeExpression
  428. AstTypeWordExpression
  429. AstTypeWordCallop
  430. AstTypeOperation
  431. AstTypeOperations
  432. AstTypeWordCall
  433. AstTypeValueExpression
  434. AstTypeValueCallop
  435. AstTypeValueCall
  436. AstTypeParametersNonempty
  437. AstTypeParameters
  438. AstTypeParameter
  439. AstTypeBlock
  440. AstTypeWordValue
  441. AstTypeWord
  442. AstTypeValue
  443. AstTypeEox
  444. AstTypeOperator
  445. AstTypeError
  446. )
  447. var astTypeMap map[AstType]string = map[AstType]string{
  448. AstTypeProgram: "AstTypeProgram",
  449. AstTypeStatements: "AstTypeStatements",
  450. AstTypeStatement: "AstTypeStatement:",
  451. AstTypeDefinition: "AstTypeDefinition",
  452. AstTypeWords: "AstTypeWords",
  453. AstTypeExpression: "AstTypeExpression",
  454. AstTypeWordExpression: "AstTypeWordExpression",
  455. AstTypeWordCallop: "AstTypeWordCallop",
  456. AstTypeOperation: "AstTypeOperation",
  457. AstTypeOperations: "AstTypeOperations",
  458. AstTypeWordCall: "AstTypeWordCall",
  459. AstTypeValueExpression: "AstTypeValueExpression",
  460. AstTypeValueCallop: "AstTypeValueCallop",
  461. AstTypeValueCall: "AstTypeValueCall",
  462. AstTypeParametersNonempty: "AstTypeParametersNonempty",
  463. AstTypeParameters: "AstTypeParameters",
  464. AstTypeParameter: "AstTypeParameter",
  465. AstTypeBlock: "AstTypeBlock",
  466. AstTypeWordValue: "AstTypeWordValue",
  467. AstTypeWord: "AstTypeWord",
  468. AstTypeValue: "AstTypeValue",
  469. AstTypeEox: "AstTypeEox",
  470. AstTypeOperator: "AstTypeOperator",
  471. AstTypeError: "AstTypeError",
  472. }
  473. func (me AstType) String() string {
  474. name, found := astTypeMap[me]
  475. if found {
  476. return name
  477. } else {
  478. return fmt.Sprintf("Unknown AstType %d", int(me))
  479. }
  480. }
  481. type Ast struct {
  482. tree.Node
  483. AstType
  484. *Token
  485. }
  486. func (me *Ast) NewChild(kind AstType, token *Token) *Ast {
  487. child := &Ast{}
  488. child.AstType = kind
  489. child.Token = token
  490. tree.AppendChild(me, child)
  491. return child
  492. }
  493. func (me *Ast) Walk(walker func(ast *Ast) *Ast) *Ast {
  494. node_res := tree.Walk(me,
  495. func(node tree.Noder) tree.Noder {
  496. ast_res := walker(node.(*Ast))
  497. if ast_res == nil {
  498. return nil
  499. } else {
  500. return ast_res
  501. }
  502. })
  503. if node_res != nil {
  504. return node_res.(*Ast)
  505. } else {
  506. return nil
  507. }
  508. }
  509. func (me *Ast) Remove() {
  510. _ = tree.Remove(me)
  511. }
  512. func NewAst(kind AstType) *Ast {
  513. ast := &Ast{}
  514. ast.AstType = kind
  515. ast.Token = nil
  516. return ast
  517. }
  518. type ParseAction func(parser *Parser) bool
  519. type RuleType int
  520. const (
  521. RuleTypeNone = RuleType(iota)
  522. RuleTypeAlternate
  523. RuleTypeSequence
  524. )
  525. type Rule struct {
  526. tree.Node
  527. Name string
  528. RuleType
  529. ParseAction
  530. }
  531. func NewRule(name string, ruty RuleType) *Rule {
  532. res := &Rule{}
  533. res.RuleType = ruty
  534. res.Name = name
  535. return res
  536. }
  537. func (me *Rule) NewChild(action ParseAction) *Rule {
  538. child := NewRule("foo", RuleTypeNone)
  539. tree.AppendChild(me, child)
  540. return child
  541. }
  542. func (me *Rule) Walk(walker func(rule *Rule) *Rule) *Rule {
  543. node_res := tree.Walk(me,
  544. func(node tree.Noder) tree.Noder {
  545. rule_res := walker(node.(*Rule))
  546. if rule_res == nil {
  547. return nil
  548. } else {
  549. return rule_res
  550. }
  551. })
  552. return node_res.(*Rule)
  553. }
  554. type Parser struct {
  555. *Ast
  556. *Lexer
  557. now *Ast
  558. lookahead *Token
  559. }
  560. func (me *Parser) SetupRules() {
  561. }
  562. func (me *Parser) Expect(types ...TokenType) bool {
  563. fmt.Print("Expecting: ", types, " from ", me.now.AstType, " have ", me.LookaheadType(), " \n")
  564. for _, t := range types {
  565. if me.LookaheadType() == t {
  566. fmt.Print("Found: ", t, "\n")
  567. return true
  568. }
  569. }
  570. fmt.Print("Not found.\n")
  571. return false
  572. }
  573. type Parsable interface {
  574. isParsable()
  575. }
  576. func (me TokenType) isParsable() {
  577. }
  578. func (me ParseAction) isParsable() {
  579. }
  580. /* Advance the lexer but only of there is no lookahead token already available in me.lookahead.
  581. */
  582. func (me *Parser) Advance() *Token {
  583. if me.lookahead == nil {
  584. me.lookahead = <-me.Lexer.Output
  585. }
  586. return me.lookahead
  587. }
  588. func (me *Parser) DropLookahead() {
  589. me.lookahead = nil
  590. }
  591. func (me *Parser) Lookahead() *Token {
  592. return me.lookahead
  593. }
  594. func (me *Parser) LookaheadType() TokenType {
  595. if me.lookahead == nil {
  596. return TokenError
  597. }
  598. return me.Lookahead().TokenType
  599. }
  600. func (me *Parser) Consume(atyp AstType, types ...TokenType) bool {
  601. me.Advance()
  602. res := me.Expect(types...)
  603. if res {
  604. me.NewAstChild(atyp)
  605. me.DropLookahead()
  606. }
  607. return res
  608. }
  609. /*
  610. func (me * Parser) OneOf(restype AstType, options ...Parsable) bool {
  611. res := false
  612. k, v := range options {
  613. switch option := v.Type {
  614. case TokenType: res := Consume(restype, option)
  615. case ParseAction: res := option(me)
  616. }
  617. }
  618. return res
  619. }
  620. */
  621. func (me *Parser) ParseEOX() bool {
  622. return me.Consume(AstTypeEox, TokenEOL, TokenPeriod)
  623. }
  624. func (me *Parser) ParseValue() bool {
  625. return me.Consume(AstTypeValue, TokenString, TokenNumber, TokenSymbol)
  626. }
  627. func (me *Parser) ParseWord() bool {
  628. return me.Consume(AstTypeWord, TokenWord)
  629. }
  630. func (me *Parser) ParseWordValue() bool {
  631. me.NewAstChildDescend(AstTypeWordValue)
  632. res := me.ParseValue() || me.ParseWord()
  633. me.AstAscend(res)
  634. return res
  635. }
  636. func (me *Parser) ParseParameter() bool {
  637. me.NewAstChildDescend(AstTypeParameter)
  638. res := me.ParseWordValue() || me.ParseBlock()
  639. me.AstAscend(res)
  640. return res
  641. }
  642. func (me *Parser) ParseParametersNonempty() bool {
  643. res := false
  644. for me.ParseParameter() {
  645. res = true
  646. }
  647. return res
  648. }
  649. func (me *Parser) ParseParameters() bool {
  650. me.NewAstChildDescend(AstTypeParameters)
  651. _ = me.ParseParametersNonempty()
  652. me.AstAscend(true)
  653. return true
  654. }
  655. func (me *Parser) ParseWordCall() bool {
  656. me.NewAstChildDescend(AstTypeWordCall)
  657. res := me.ParseParameters() && me.ParseEOX()
  658. me.AstAscend(res)
  659. return res
  660. }
  661. func (me *Parser) ParseOperator() bool {
  662. return me.Consume(AstTypeOperator, TokenOperator)
  663. }
  664. func (me *Parser) ParseOperation() bool {
  665. me.NewAstChildDescend(AstTypeOperation)
  666. res := me.ParseOperator() && me.ParseParametersNonempty()
  667. me.AstAscend(res)
  668. return res
  669. }
  670. func (me *Parser) ParseOperations() bool {
  671. me.NewAstChildDescend(AstTypeOperations)
  672. res := me.ParseOperation()
  673. for me.ParseOperation() {
  674. }
  675. me.AstAscend(res)
  676. return res
  677. }
  678. func (me *Parser) ParseWordCallOp() bool {
  679. me.NewAstChildDescend(AstTypeWordCallop)
  680. res := me.ParseWordCall() || me.ParseOperations()
  681. me.AstAscend(res)
  682. return res
  683. }
  684. func (me *Parser) ParseWordExpression() bool {
  685. me.NewAstChildDescend(AstTypeWordExpression)
  686. res := me.ParseWord() && me.ParseWordCallOp()
  687. me.AstAscend(res)
  688. return res
  689. }
  690. func (me *Parser) ParseValueCall() bool {
  691. me.NewAstChildDescend(AstTypeValueCall)
  692. res := me.ParseParameters() && me.ParseEOX()
  693. me.AstAscend(res)
  694. return res
  695. }
  696. func (me *Parser) ParseValueCallOp() bool {
  697. me.NewAstChildDescend(AstTypeValueCallop)
  698. res := me.ParseValueCall() || me.ParseOperations()
  699. me.AstAscend(res)
  700. return res
  701. }
  702. func (me *Parser) ParseValueExpression() bool {
  703. me.NewAstChildDescend(AstTypeValueExpression)
  704. res := me.ParseValue() && me.ParseValueCallOp()
  705. me.AstAscend(res)
  706. return false
  707. }
  708. func (me *Parser) NewAstChild(tyty AstType) *Ast {
  709. return me.now.NewChild(tyty, me.lookahead)
  710. }
  711. func (me *Parser) NewAstChildDescend(tyty AstType) {
  712. node := me.NewAstChild(tyty)
  713. me.now = node
  714. }
  715. func (me *Parser) AstAscend(keep bool) {
  716. if me.now.Parent() != nil {
  717. now := me.now
  718. me.now = now.Parent().(*Ast)
  719. if !keep {
  720. now.Remove()
  721. }
  722. }
  723. }
  724. func (me TokenType) CloseForOpen() (TokenType, bool) {
  725. switch me {
  726. case TokenOpenBrace:
  727. return TokenCloseBrace, true
  728. case TokenOpenBracket:
  729. return TokenCloseBracket, true
  730. case TokenOpenParen:
  731. return TokenCloseParen, true
  732. case TokenKeywordDo:
  733. return TokenKeywordEnd, true
  734. default:
  735. return TokenError, false
  736. }
  737. }
  738. func (me *Parser) ParseBlock() bool {
  739. me.Advance()
  740. open := me.LookaheadType()
  741. done, ok := open.CloseForOpen()
  742. if !ok {
  743. /* Not an opening of a block, so no block found. */
  744. return false
  745. }
  746. me.DropLookahead()
  747. me.NewAstChildDescend(AstTypeBlock)
  748. res := me.ParseStatements()
  749. me.AstAscend(res)
  750. if res {
  751. me.Advance()
  752. if me.LookaheadType() != done {
  753. return me.ParseError()
  754. }
  755. me.DropLookahead()
  756. }
  757. return res
  758. }
  759. func (me *Parser) ParseWords() bool {
  760. me.NewAstChildDescend(AstTypeWords)
  761. res := me.ParseWord()
  762. for me.ParseWord() {
  763. }
  764. me.AstAscend(res)
  765. return res
  766. }
  767. func (me *Parser) ParseDefinition() bool {
  768. me.Advance()
  769. res := me.Consume(AstTypeDefinition, TokenKeywordTo)
  770. if !res {
  771. return false
  772. }
  773. res = res && me.ParseWords()
  774. if !res {
  775. _ = me.ParseError()
  776. }
  777. res = res && me.ParseBlock()
  778. if !res {
  779. _ = me.ParseError()
  780. }
  781. me.AstAscend(true)
  782. return res
  783. }
  784. func (me *Parser) ParseError() bool {
  785. me.now.NewChild(AstTypeError, me.lookahead)
  786. fmt.Printf("Parse error: at %s\n", me.lookahead)
  787. return false
  788. }
  789. func (me *Parser) ParseExpression() bool {
  790. return me.ParseWordExpression() || me.ParseValueExpression()
  791. }
  792. func (me *Parser) ParseStatement() bool {
  793. me.NewAstChildDescend(AstTypeStatement)
  794. /* First case is for an empty expression/statement. */
  795. res := me.ParseEOX() || me.ParseDefinition() || me.ParseExpression() || me.ParseBlock()
  796. me.AstAscend(res)
  797. return res
  798. }
  799. func (me *Parser) ParseEOF() bool {
  800. return me.Consume(AstTypeEox, TokenEOF)
  801. }
  802. func (me *Parser) ParseStatements() bool {
  803. me.NewAstChildDescend(AstTypeStatements)
  804. res := me.ParseStatement()
  805. for me.ParseStatement() {
  806. }
  807. me.AstAscend(res)
  808. return res
  809. }
  810. func (me *Parser) ParseProgram() bool {
  811. return me.ParseStatements() && me.ParseEOF()
  812. }
  813. func NewParserForLexer(lexer *Lexer) *Parser {
  814. me := &Parser{}
  815. me.Ast = NewAst(AstTypeProgram)
  816. me.now = me.Ast
  817. me.Lexer = lexer
  818. me.Ast.Token = &Token{}
  819. go me.Lexer.Start()
  820. return me
  821. }
  822. func NewParserForText(text string) *Parser {
  823. lexer := OpenLexer(strings.NewReader(text))
  824. return NewParserForLexer(lexer)
  825. }
  826. func (me *Ast) DotID() string {
  827. return fmt.Sprintf("ast_%p", me)
  828. }
  829. func (me *Ast) Dotty() {
  830. g := graphviz.NewDigraph("rankdir", "LR")
  831. me.Walk(func(ast *Ast) *Ast {
  832. label := ast.AstType.String()
  833. if ast.Token != nil {
  834. label = label + "\n" + ast.Token.String()
  835. }
  836. g.AddNode(ast.DotID(), "label", label)
  837. if ast.Parent() != nil {
  838. g.AddEdgeByName(ast.Parent().(*Ast).DotID(), ast.DotID())
  839. }
  840. return nil
  841. })
  842. g.Dotty()
  843. }
  844. /*
  845. PROGRAM -> STATEMENTS.
  846. STATEMENTS -> STATEMENT STATEMENTS | .
  847. STATEMENT -> DEFINITION | EXPRESSION | BLOCK .
  848. DEFINITION -> to WORDS BLOCK.
  849. WORDS -> word WORDS | .
  850. EXPRESSION -> WORD_EXPRESSION | VALUE_EXPRESSION.
  851. WORD_EXPRESSION -> word WORD_CALLOP.
  852. WORD_CALLOP -> WORD_OPERATION | WORD_CALL.
  853. OPERATION -> operator PARAMETERS_NONEMPTY EOX.
  854. WORD_CALL -> PARAMETERS EOX.
  855. VALUE_EXPRESSION -> value VALUE_CALLOP.
  856. VALUE_CALLOP -> VALUE_OPERATION | VALUE_CALL.
  857. VALUE_CALL -> EOX.
  858. PARAMETERS_NONEMPTY -> PARAMETER PARAMETERS.
  859. PARAMETERS -> PARAMETERS_NONEMPTY | .
  860. PARAMETER -> BLOCK | WORDVALUE | OPERATION.
  861. BLOCK -> ob STATEMENTS cb | op STATEMENTS cp | oa STATEMENTS ca | do STATEMENTS end.
  862. WORDVALUE -> word | VALUE.
  863. VALUE -> string | number | symbol.
  864. EOX -> eol | period.
  865. AstNodeBlock = AstNodeType(iota)
  866. )
  867. */
  868. type Environment struct {
  869. Parent *Environment
  870. }
  871. func main() {
  872. fmt.Println("Hello World!")
  873. }