peg.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package raku
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "github.com/yhirose/go-peg"
  8. )
  9. type Parser struct {
  10. *peg.Parser
  11. }
  12. type Result struct {
  13. * peg.Ast
  14. }
  15. func parserCheck(err error) {
  16. if perr, ok := err.(*peg.Error); ok {
  17. for _, d := range perr.Details {
  18. fmt.Println(d)
  19. }
  20. os.Exit(1)
  21. }
  22. }
  23. var defaultParser *Parser
  24. /** Loads a PEG parser from the given file name. */
  25. func LoadParser(filename string) (*Parser, error) {
  26. result := &Parser{}
  27. data, err := ioutil.ReadFile(filename)
  28. if err == nil {
  29. result.Parser, err = peg.NewParser(string(data))
  30. if err == nil {
  31. result.EnableAst()
  32. }
  33. return result, err
  34. } else {
  35. return nil, err
  36. }
  37. }
  38. func InitDefaultParser(peg_filename string) error {
  39. var err error
  40. defaultParser, err = LoadParser(peg_filename)
  41. return err
  42. }
  43. func wrapResult(wrapme peg.Any) *Result {
  44. result := &Result{};
  45. ast, ok := wrapme.(*peg.Ast)
  46. if ok {
  47. result.Ast = ast
  48. return result
  49. }
  50. return nil
  51. }
  52. func (parser Parser) Parse(source string) (*Result, error) {
  53. ast, err := parser.ParseAndGetValue(source, nil)
  54. return wrapResult(ast), err
  55. }
  56. func (parser Parser) ParseFile(filename string) (*Result, error) {
  57. source, err := ioutil.ReadFile(filename)
  58. if err == nil {
  59. return parser.Parse(string(source))
  60. } else {
  61. return nil, err
  62. }
  63. }
  64. func parse(source string) (*Result, error) {
  65. if defaultParser == nil {
  66. return nil, errors.New("Default parser not initialized!")
  67. }
  68. return defaultParser.Parse(source)
  69. }
  70. func parseFile(filename string) (*Result, error) {
  71. if defaultParser == nil {
  72. return nil, errors.New("Default parser not initialized!")
  73. }
  74. return defaultParser.ParseFile(filename)
  75. }
  76. type Method func (raku * Raku, message string, target Object, args ... interface{}) (Object, error)
  77. type Command struct {
  78. Name string
  79. Method
  80. }
  81. type CommandMap = map[string] Command
  82. type Class interface {
  83. Object
  84. Parent() * Class
  85. Selector(name string) * Method
  86. }
  87. type Object interface {
  88. Class() Class
  89. Send(raku * Raku, message string, args ... interface {}) (Object, error)
  90. }
  91. var RootClass * DefaultClass = &DefaultClass{}
  92. type DefaultObject struct {
  93. class Class
  94. * Raku
  95. CommandMap
  96. }
  97. func (object DefaultObject) Selector(name string) Method {
  98. command, ok := object.CommandMap[name]
  99. if !ok {
  100. method := object.class.Selector(name)
  101. if method == nil {
  102. return nil
  103. }
  104. return method
  105. }
  106. return command.Method
  107. }
  108. func (object DefaultObject) Class() Object {
  109. return RootClass
  110. }
  111. const METHOD_MISSING = "method_missing"
  112. func (object DefaultObject) Send(raku * Raku, message string, args ... interface {}) (Object, error) {
  113. method := object.Selector(message)
  114. if (method != nil) {
  115. return method(object.Raku, message, object, args ...)
  116. }
  117. method = object.Selector(METHOD_MISSING)
  118. if (method != nil) {
  119. return method(object.Raku, message, object, args ...)
  120. }
  121. return nil, fmt.Errorf("Cannot send message %s to object %v.", message, object)
  122. }
  123. type DefaultClass struct {
  124. parent * Class
  125. DefaultObject
  126. }
  127. func (class DefaultClass) Parent() Object {
  128. return class.parent
  129. }
  130. type NilClass struct {
  131. DefaultClass
  132. }
  133. var Nil Object = &DefaultObject{NilClass};
  134. var Root * Object = &DefaultObject{RootClass}
  135. type Boolean struct {
  136. DefaultObject
  137. }
  138. var True * Boolean = &Boolean{Root}
  139. var False * Boolean = &Boolean{Root}
  140. func (boolean Boolean) Parent() Object {
  141. return Nil
  142. }
  143. func (boolean Boolean) Send(raku * Raku, message string, args ... interface {}) (Object, error) {
  144. return Root.Send(raku, message, args ...);
  145. }
  146. func (method Method) Parent() Object {
  147. return Nil
  148. }
  149. func (method Method) Send(raku * Raku, message string, args ... interface {}) (Object, error) {
  150. return method(raku, message, method, args...)
  151. }
  152. type Raku struct {
  153. * Parser
  154. CommandMap
  155. Root * Object
  156. Nil * NilClass
  157. True * Boolean
  158. False * Boolean
  159. }
  160. func New(peg_filename string) (*Raku, error) {
  161. var err error
  162. result := &Raku{}
  163. result.Parser, err = LoadParser(peg_filename)
  164. if (err == nil) {
  165. }
  166. return result, err
  167. }
  168. func Evaluate(raku * Raku, result Result) {
  169. _
  170. }