modernc.org/cc@v1.0.1/global.go (about) 1 // Copyright 2016 The CC Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cc // import "modernc.org/cc" 6 7 import ( 8 "reflect" 9 "strconv" 10 "strings" 11 12 "modernc.org/golex/lex" 13 "modernc.org/strutil" 14 "modernc.org/xc" 15 ) 16 17 const ( 18 commentNotClosed = "comment not closed" 19 ) 20 21 var ( 22 debugIncludes bool 23 debugMacros bool 24 dict = xc.Dict 25 fset = xc.FileSet 26 isGenerating bool // go generate hook. 27 isTesting bool // Test hook. 28 printHooks = strutil.PrettyPrintHooks{} 29 ) 30 31 func init() { 32 for k, v := range xc.PrintHooks { 33 printHooks[k] = v 34 } 35 lcRT := reflect.TypeOf(lex.Char{}) 36 lcH := func(f strutil.Formatter, v interface{}, prefix, suffix string) { 37 c := v.(lex.Char) 38 r := c.Rune 39 s := yySymName(int(r)) 40 if x := s[0]; x >= '0' && x <= '9' { 41 s = strconv.QuoteRune(r) 42 } 43 f.Format("%s%v: %s"+suffix, prefix, fset.Position(c.Pos()), s) 44 } 45 46 printHooks[lcRT] = lcH 47 printHooks[reflect.TypeOf(xc.Token{})] = func(f strutil.Formatter, v interface{}, prefix, suffix string) { 48 t := v.(xc.Token) 49 if (t == xc.Token{}) { 50 return 51 } 52 53 lcH(f, t.Char, prefix, "") 54 if s := t.S(); len(s) != 0 { 55 f.Format(" %q", s) 56 } 57 f.Format(suffix) 58 } 59 60 printHooks[reflect.TypeOf(PPTokenList(0))] = func(f strutil.Formatter, v interface{}, prefix, suffix string) { 61 x := v.(PPTokenList) 62 if x == 0 { 63 return 64 } 65 66 a := strings.Split(prefix+PrettyString(decodeTokens(x, nil, true))+",", "\n") 67 for _, v := range a { 68 f.Format("%s\n", v) 69 } 70 71 } 72 73 printHooks[reflect.TypeOf((*ctype)(nil))] = func(f strutil.Formatter, v interface{}, prefix, suffix string) { 74 f.Format(prefix) 75 f.Format("%v", v.(Type).String()) 76 f.Format(suffix) 77 } 78 79 printHooks[reflect.TypeOf(Kind(0))] = func(f strutil.Formatter, v interface{}, prefix, suffix string) { 80 f.Format(prefix) 81 f.Format("%v", v.(Kind)) 82 f.Format(suffix) 83 } 84 85 printHooks[reflect.TypeOf(Linkage(0))] = func(f strutil.Formatter, v interface{}, prefix, suffix string) { 86 f.Format(prefix) 87 f.Format("%v", v.(Linkage)) 88 f.Format(suffix) 89 } 90 }