summaryrefslogtreecommitdiffstats
path: root/database.go
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2016-02-28 11:12:07 -0500
committerThibaut Horel <thibaut.horel@gmail.com>2016-02-28 11:12:07 -0500
commit97cd594897f2b35e824bcc4902ca2300c434569f (patch)
tree1ee1420e2dad5c75faedde46d79b08f69d5fbf07 /database.go
parent965edb3dc0b3f6780c6763219832ac6855098b82 (diff)
downloadbibtex-97cd594897f2b35e824bcc4902ca2300c434569f.tar.gz
Parse tree now contains unflattened values
Diffstat (limited to 'database.go')
-rw-r--r--database.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/database.go b/database.go
new file mode 100644
index 0000000..57d2008
--- /dev/null
+++ b/database.go
@@ -0,0 +1,73 @@
+package bibtex
+
+import (
+ "bytes"
+ "strconv"
+ "strings"
+)
+
+type Literal interface{}
+
+type BraceLiteral string
+type StringLiteral string
+type NumberLiteral int
+type VariableLiteral struct {
+ Name string
+ Value *Value
+}
+
+func Marshal(l Literal) (res string) {
+ switch v := l.(type) {
+ case BraceLiteral:
+ res = "{" + string(v) + "}"
+ case StringLiteral:
+ res = "\"" + string(v) + "\""
+ case NumberLiteral:
+ res = strconv.Itoa(int(v))
+ case VariableLiteral:
+ res = v.Name
+ }
+ return
+}
+
+type Value []Literal
+
+func (v Value) Marshal() string {
+ res := make([]string, len(v))
+ for i, l := range v {
+ res[i] = Marshal(l)
+ }
+ return strings.Join(res, " # ")
+}
+
+func (v Value) String() string {
+ var buf bytes.Buffer
+ for _, l := range v {
+ switch lit := l.(type) {
+ case BraceLiteral:
+ buf.WriteString(string(lit))
+ case StringLiteral:
+ buf.WriteString(string(lit))
+ case NumberLiteral:
+ buf.WriteString(strconv.Itoa(int(lit)))
+ case VariableLiteral:
+ buf.WriteString(lit.Value.String())
+ }
+ }
+ return buf.String()
+}
+
+type Entry struct {
+ Type string
+ Key string
+ Fields map[string]Value
+ FNames []string
+}
+
+type Database struct {
+ SNames []string
+ Strings map[string]Value
+ Entries map[string]Entry
+ Preamble Value
+ EKeys []string
+}