ast.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. //Copyright 2013 GoGraphviz Authors
  2. //
  3. //Licensed under the Apache License, Version 2.0 (the "License");
  4. //you may not use this file except in compliance with the License.
  5. //You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. //Unless required by applicable law or agreed to in writing, software
  10. //distributed under the License is distributed on an "AS IS" BASIS,
  11. //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. //See the License for the specific language governing permissions and
  13. //limitations under the License.
  14. //Abstract Syntax Tree representing the DOT grammar
  15. package ast
  16. import (
  17. "errors"
  18. "fmt"
  19. "math/rand"
  20. "sort"
  21. "strings"
  22. "github.com/awalterschulze/gographviz/internal/token"
  23. )
  24. var (
  25. r = rand.New(rand.NewSource(1234))
  26. )
  27. type Visitor interface {
  28. Visit(e Elem) Visitor
  29. }
  30. type Elem interface {
  31. String() string
  32. }
  33. type Walkable interface {
  34. Walk(v Visitor)
  35. }
  36. type Attrib interface{}
  37. type Bool bool
  38. const (
  39. FALSE = Bool(false)
  40. TRUE = Bool(true)
  41. )
  42. func (this Bool) String() string {
  43. if this {
  44. return "true"
  45. }
  46. return "false"
  47. }
  48. func (this Bool) Walk(v Visitor) {
  49. if v == nil {
  50. return
  51. }
  52. v.Visit(this)
  53. }
  54. type GraphType bool
  55. const (
  56. GRAPH = GraphType(false)
  57. DIGRAPH = GraphType(true)
  58. )
  59. func (this GraphType) String() string {
  60. if this {
  61. return "digraph"
  62. }
  63. return "graph"
  64. }
  65. func (this GraphType) Walk(v Visitor) {
  66. if v == nil {
  67. return
  68. }
  69. v.Visit(this)
  70. }
  71. type Graph struct {
  72. Type GraphType
  73. Strict bool
  74. ID ID
  75. StmtList StmtList
  76. }
  77. func NewGraph(t, strict, id, l Attrib) (*Graph, error) {
  78. g := &Graph{Type: t.(GraphType), Strict: bool(strict.(Bool)), ID: ID("")}
  79. if id != nil {
  80. g.ID = id.(ID)
  81. }
  82. if l != nil {
  83. g.StmtList = l.(StmtList)
  84. }
  85. return g, nil
  86. }
  87. func (this *Graph) String() string {
  88. var s string
  89. if this.Strict {
  90. s += "strict "
  91. }
  92. s += this.Type.String() + " " + this.ID.String() + " {\n"
  93. if this.StmtList != nil {
  94. s += this.StmtList.String()
  95. }
  96. s += "\n}\n"
  97. return s
  98. }
  99. func (this *Graph) Walk(v Visitor) {
  100. if v == nil {
  101. return
  102. }
  103. v = v.Visit(this)
  104. this.Type.Walk(v)
  105. this.ID.Walk(v)
  106. this.StmtList.Walk(v)
  107. }
  108. type StmtList []Stmt
  109. func NewStmtList(s Attrib) (StmtList, error) {
  110. ss := make(StmtList, 1)
  111. ss[0] = s.(Stmt)
  112. return ss, nil
  113. }
  114. func AppendStmtList(ss, s Attrib) (StmtList, error) {
  115. this := ss.(StmtList)
  116. this = append(this, s.(Stmt))
  117. return this, nil
  118. }
  119. func (this StmtList) String() string {
  120. if len(this) == 0 {
  121. return ""
  122. }
  123. s := ""
  124. for i := 0; i < len(this); i++ {
  125. ss := this[i].String()
  126. if len(ss) > 0 {
  127. s += "\t" + ss + ";\n"
  128. }
  129. }
  130. return s
  131. }
  132. func (this StmtList) Walk(v Visitor) {
  133. if v == nil {
  134. return
  135. }
  136. v = v.Visit(this)
  137. for i := range this {
  138. this[i].Walk(v)
  139. }
  140. }
  141. type Stmt interface {
  142. Elem
  143. Walkable
  144. isStmt()
  145. }
  146. func (this NodeStmt) isStmt() {}
  147. func (this EdgeStmt) isStmt() {}
  148. func (this EdgeAttrs) isStmt() {}
  149. func (this NodeAttrs) isStmt() {}
  150. func (this GraphAttrs) isStmt() {}
  151. func (this *SubGraph) isStmt() {}
  152. func (this *Attr) isStmt() {}
  153. type SubGraph struct {
  154. ID ID
  155. StmtList StmtList
  156. }
  157. func NewSubGraph(id, l Attrib) (*SubGraph, error) {
  158. g := &SubGraph{ID: ID(fmt.Sprintf("anon%d", r.Int63()))}
  159. if id != nil {
  160. if len(id.(ID)) > 0 {
  161. g.ID = id.(ID)
  162. }
  163. }
  164. if l != nil {
  165. g.StmtList = l.(StmtList)
  166. }
  167. return g, nil
  168. }
  169. func (this *SubGraph) GetID() ID {
  170. return this.ID
  171. }
  172. func (this *SubGraph) GetPort() Port {
  173. return NewPort(nil, nil)
  174. }
  175. func (this *SubGraph) String() string {
  176. gName := this.ID.String()
  177. if strings.HasPrefix(gName, "anon") {
  178. gName = ""
  179. }
  180. s := "subgraph " + this.ID.String() + " {\n"
  181. if this.StmtList != nil {
  182. s += this.StmtList.String()
  183. }
  184. s += "\n}\n"
  185. return s
  186. }
  187. func (this *SubGraph) Walk(v Visitor) {
  188. if v == nil {
  189. return
  190. }
  191. v = v.Visit(this)
  192. this.ID.Walk(v)
  193. this.StmtList.Walk(v)
  194. }
  195. type EdgeAttrs AttrList
  196. func NewEdgeAttrs(a Attrib) (EdgeAttrs, error) {
  197. return EdgeAttrs(a.(AttrList)), nil
  198. }
  199. func (this EdgeAttrs) String() string {
  200. s := AttrList(this).String()
  201. if len(s) == 0 {
  202. return ""
  203. }
  204. return `edge ` + s
  205. }
  206. func (this EdgeAttrs) Walk(v Visitor) {
  207. if v == nil {
  208. return
  209. }
  210. v = v.Visit(this)
  211. for i := range this {
  212. this[i].Walk(v)
  213. }
  214. }
  215. type NodeAttrs AttrList
  216. func NewNodeAttrs(a Attrib) (NodeAttrs, error) {
  217. return NodeAttrs(a.(AttrList)), nil
  218. }
  219. func (this NodeAttrs) String() string {
  220. s := AttrList(this).String()
  221. if len(s) == 0 {
  222. return ""
  223. }
  224. return `node ` + s
  225. }
  226. func (this NodeAttrs) Walk(v Visitor) {
  227. if v == nil {
  228. return
  229. }
  230. v = v.Visit(this)
  231. for i := range this {
  232. this[i].Walk(v)
  233. }
  234. }
  235. type GraphAttrs AttrList
  236. func NewGraphAttrs(a Attrib) (GraphAttrs, error) {
  237. return GraphAttrs(a.(AttrList)), nil
  238. }
  239. func (this GraphAttrs) String() string {
  240. s := AttrList(this).String()
  241. if len(s) == 0 {
  242. return ""
  243. }
  244. return `graph ` + s
  245. }
  246. func (this GraphAttrs) Walk(v Visitor) {
  247. if v == nil {
  248. return
  249. }
  250. v = v.Visit(this)
  251. for i := range this {
  252. this[i].Walk(v)
  253. }
  254. }
  255. type AttrList []AList
  256. func NewAttrList(a Attrib) (AttrList, error) {
  257. as := make(AttrList, 0)
  258. if a != nil {
  259. as = append(as, a.(AList))
  260. }
  261. return as, nil
  262. }
  263. func AppendAttrList(as, a Attrib) (AttrList, error) {
  264. this := as.(AttrList)
  265. if a == nil {
  266. return this, nil
  267. }
  268. this = append(this, a.(AList))
  269. return this, nil
  270. }
  271. func (this AttrList) String() string {
  272. s := ""
  273. for _, alist := range this {
  274. ss := alist.String()
  275. if len(ss) > 0 {
  276. s += "[ " + ss + " ] "
  277. }
  278. }
  279. if len(s) == 0 {
  280. return ""
  281. }
  282. return s
  283. }
  284. func (this AttrList) Walk(v Visitor) {
  285. if v == nil {
  286. return
  287. }
  288. v = v.Visit(this)
  289. for i := range this {
  290. this[i].Walk(v)
  291. }
  292. }
  293. func PutMap(attrmap map[string]string) AttrList {
  294. attrlist := make(AttrList, 1)
  295. attrlist[0] = make(AList, 0)
  296. keys := make([]string, 0, len(attrmap))
  297. for key := range attrmap {
  298. keys = append(keys, key)
  299. }
  300. sort.Strings(keys)
  301. for _, name := range keys {
  302. value := attrmap[name]
  303. attrlist[0] = append(attrlist[0], &Attr{ID(name), ID(value)})
  304. }
  305. return attrlist
  306. }
  307. func (this AttrList) GetMap() map[string]string {
  308. attrs := make(map[string]string)
  309. for _, alist := range this {
  310. for _, attr := range alist {
  311. attrs[attr.Field.String()] = attr.Value.String()
  312. }
  313. }
  314. return attrs
  315. }
  316. type AList []*Attr
  317. func NewAList(a Attrib) (AList, error) {
  318. as := make(AList, 1)
  319. as[0] = a.(*Attr)
  320. return as, nil
  321. }
  322. func AppendAList(as, a Attrib) (AList, error) {
  323. this := as.(AList)
  324. attr := a.(*Attr)
  325. this = append(this, attr)
  326. return this, nil
  327. }
  328. func (this AList) String() string {
  329. if len(this) == 0 {
  330. return ""
  331. }
  332. str := this[0].String()
  333. for i := 1; i < len(this); i++ {
  334. str += `, ` + this[i].String()
  335. }
  336. return str
  337. }
  338. func (this AList) Walk(v Visitor) {
  339. v = v.Visit(this)
  340. for i := range this {
  341. this[i].Walk(v)
  342. }
  343. }
  344. type Attr struct {
  345. Field ID
  346. Value ID
  347. }
  348. func NewAttr(f, v Attrib) (*Attr, error) {
  349. a := &Attr{Field: f.(ID)}
  350. a.Value = ID("true")
  351. if v != nil {
  352. ok := false
  353. a.Value, ok = v.(ID)
  354. if !ok {
  355. return nil, errors.New(fmt.Sprintf("value = %v", v))
  356. }
  357. }
  358. return a, nil
  359. }
  360. func (this *Attr) String() string {
  361. return this.Field.String() + `=` + this.Value.String()
  362. }
  363. func (this *Attr) Walk(v Visitor) {
  364. if v == nil {
  365. return
  366. }
  367. v = v.Visit(this)
  368. this.Field.Walk(v)
  369. this.Value.Walk(v)
  370. }
  371. type Location interface {
  372. Elem
  373. Walkable
  374. isLocation()
  375. GetID() ID
  376. GetPort() Port
  377. IsNode() bool
  378. }
  379. func (this *NodeID) isLocation() {}
  380. func (this *NodeID) IsNode() bool { return true }
  381. func (this *SubGraph) isLocation() {}
  382. func (this *SubGraph) IsNode() bool { return false }
  383. type EdgeStmt struct {
  384. Source Location
  385. EdgeRHS EdgeRHS
  386. Attrs AttrList
  387. }
  388. func NewEdgeStmt(id, e, attrs Attrib) (*EdgeStmt, error) {
  389. var a AttrList = nil
  390. var err error = nil
  391. if attrs == nil {
  392. a, err = NewAttrList(nil)
  393. if err != nil {
  394. return nil, err
  395. }
  396. } else {
  397. a = attrs.(AttrList)
  398. }
  399. return &EdgeStmt{id.(Location), e.(EdgeRHS), a}, nil
  400. }
  401. func (this EdgeStmt) String() string {
  402. return strings.TrimSpace(this.Source.String() + this.EdgeRHS.String() + this.Attrs.String())
  403. }
  404. func (this EdgeStmt) Walk(v Visitor) {
  405. if v == nil {
  406. return
  407. }
  408. v = v.Visit(this)
  409. this.Source.Walk(v)
  410. this.EdgeRHS.Walk(v)
  411. this.Attrs.Walk(v)
  412. }
  413. type EdgeRHS []*EdgeRH
  414. func NewEdgeRHS(op, id Attrib) (EdgeRHS, error) {
  415. return EdgeRHS{&EdgeRH{op.(EdgeOp), id.(Location)}}, nil
  416. }
  417. func AppendEdgeRHS(e, op, id Attrib) (EdgeRHS, error) {
  418. erhs := e.(EdgeRHS)
  419. erhs = append(erhs, &EdgeRH{op.(EdgeOp), id.(Location)})
  420. return erhs, nil
  421. }
  422. func (this EdgeRHS) String() string {
  423. s := ""
  424. for i := range this {
  425. s += this[i].String()
  426. }
  427. return strings.TrimSpace(s)
  428. }
  429. func (this EdgeRHS) Walk(v Visitor) {
  430. if v == nil {
  431. return
  432. }
  433. v = v.Visit(this)
  434. for i := range this {
  435. this[i].Walk(v)
  436. }
  437. }
  438. type EdgeRH struct {
  439. Op EdgeOp
  440. Destination Location
  441. }
  442. func (this *EdgeRH) String() string {
  443. return strings.TrimSpace(this.Op.String() + this.Destination.String())
  444. }
  445. func (this *EdgeRH) Walk(v Visitor) {
  446. if v == nil {
  447. return
  448. }
  449. v = v.Visit(this)
  450. this.Op.Walk(v)
  451. this.Destination.Walk(v)
  452. }
  453. type NodeStmt struct {
  454. NodeID *NodeID
  455. Attrs AttrList
  456. }
  457. func NewNodeStmt(id, attrs Attrib) (*NodeStmt, error) {
  458. nid := id.(*NodeID)
  459. var a AttrList = nil
  460. var err error = nil
  461. if attrs == nil {
  462. a, err = NewAttrList(nil)
  463. if err != nil {
  464. return nil, err
  465. }
  466. } else {
  467. a = attrs.(AttrList)
  468. }
  469. return &NodeStmt{nid, a}, nil
  470. }
  471. func (this NodeStmt) String() string {
  472. return strings.TrimSpace(this.NodeID.String() + ` ` + this.Attrs.String())
  473. }
  474. func (this NodeStmt) Walk(v Visitor) {
  475. if v == nil {
  476. return
  477. }
  478. v = v.Visit(this)
  479. this.NodeID.Walk(v)
  480. this.Attrs.Walk(v)
  481. }
  482. type EdgeOp bool
  483. const (
  484. DIRECTED EdgeOp = true
  485. UNDIRECTED EdgeOp = false
  486. )
  487. func (this EdgeOp) String() string {
  488. if this == DIRECTED {
  489. return "->"
  490. }
  491. return "--"
  492. }
  493. func (this EdgeOp) Walk(v Visitor) {
  494. if v == nil {
  495. return
  496. }
  497. v.Visit(this)
  498. }
  499. type NodeID struct {
  500. ID ID
  501. Port Port
  502. }
  503. func NewNodeID(id, port Attrib) (*NodeID, error) {
  504. if port == nil {
  505. return &NodeID{id.(ID), Port{"", ""}}, nil
  506. }
  507. return &NodeID{id.(ID), port.(Port)}, nil
  508. }
  509. func MakeNodeID(id string, port string) *NodeID {
  510. p := Port{"", ""}
  511. if len(port) > 0 {
  512. ps := strings.Split(port, ":")
  513. p.ID1 = ID(ps[0])
  514. if len(ps) > 1 {
  515. p.ID2 = ID(ps[1])
  516. }
  517. }
  518. return &NodeID{ID(id), p}
  519. }
  520. func (this *NodeID) String() string {
  521. return this.ID.String() + this.Port.String()
  522. }
  523. func (this *NodeID) GetID() ID {
  524. return this.ID
  525. }
  526. func (this *NodeID) GetPort() Port {
  527. return this.Port
  528. }
  529. func (this *NodeID) Walk(v Visitor) {
  530. if v == nil {
  531. return
  532. }
  533. v = v.Visit(this)
  534. this.ID.Walk(v)
  535. this.Port.Walk(v)
  536. }
  537. //TODO semantic analysis should decide which ID is an ID and which is a Compass Point
  538. type Port struct {
  539. ID1 ID
  540. ID2 ID
  541. }
  542. func NewPort(id1, id2 Attrib) Port {
  543. port := Port{ID(""), ID("")}
  544. if id1 != nil {
  545. port.ID1 = id1.(ID)
  546. }
  547. if id2 != nil {
  548. port.ID2 = id2.(ID)
  549. }
  550. return port
  551. }
  552. func (this Port) String() string {
  553. if len(this.ID1) == 0 {
  554. return ""
  555. }
  556. s := ":" + this.ID1.String()
  557. if len(this.ID2) > 0 {
  558. s += ":" + this.ID2.String()
  559. }
  560. return s
  561. }
  562. func (this Port) Walk(v Visitor) {
  563. if v == nil {
  564. return
  565. }
  566. v = v.Visit(this)
  567. this.ID1.Walk(v)
  568. this.ID2.Walk(v)
  569. }
  570. type ID string
  571. func NewID(id Attrib) (ID, error) {
  572. if id == nil {
  573. return ID(""), nil
  574. }
  575. id_lit := string(id.(*token.Token).Lit)
  576. return ID(id_lit), nil
  577. }
  578. func (this ID) String() string {
  579. return string(this)
  580. }
  581. func (this ID) Walk(v Visitor) {
  582. if v == nil {
  583. return
  584. }
  585. v.Visit(this)
  586. }