You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/01/31 23:54:52 UTC

[GitHub] ccollins476ad commented on a change in pull request #122: Code for parsing and evaluating syscfg expressions

ccollins476ad commented on a change in pull request #122:     Code for parsing and evaluating syscfg expressions
URL: https://github.com/apache/mynewt-newt/pull/122#discussion_r165224470
 
 

 ##########
 File path: newt/parse/lex.go
 ##########
 @@ -0,0 +1,175 @@
+package parse
+
+import (
+	"fmt"
+	"strings"
+
+	"mynewt.apache.org/newt/util"
+)
+
+type TokenCode int
+
+const (
+	TOKEN_NOT_EQUALS TokenCode = iota
+	TOKEN_NOT
+	TOKEN_EQUALS
+	TOKEN_AND
+	TOKEN_OR
+	TOKEN_XOR
+	TOKEN_LPAREN
+	TOKEN_RPAREN
+	TOKEN_STRING
+	TOKEN_NUMBER
+	TOKEN_IDENT
+)
+
+type Token struct {
+	Code   TokenCode
+	Text   string
+	Offset int
+}
+
+// Returns length of token on success; 0 if no match.
+type LexFn func(s string) (string, int, error)
+
+const delimChars = "!='\"&|^() \t\n"
+
+func lexString(s string, sought string) (string, int, error) {
+	if strings.HasPrefix(s, sought) {
+		return sought, len(sought), nil
+	}
+
+	return "", 0, nil
+}
+
+func lexStringFn(sought string) LexFn {
+	return func(s string) (string, int, error) { return lexString(s, sought) }
+}
+
+func lexLitNumber(s string) (string, int, error) {
+	var sub string
+	idx := strings.IndexAny(s, delimChars)
+	if idx == -1 {
+		sub = s
+	} else {
+		sub = s[:idx]
+	}
+	if _, ok := util.AtoiNoOctTry(sub); ok {
+		return sub, len(sub), nil
+	}
+
+	return "", 0, nil
+}
+
+func lexLitString(s string) (string, int, error) {
+	if s[0] != '"' {
+		return "", 0, nil
+	}
+
+	quote2 := strings.IndexByte(s[1:], '"')
+	if quote2 == -1 {
+		return "", 0, fmt.Errorf("unterminated quote: %s", s)
+	}
+
+	return s[1 : quote2+1], quote2 + 2, nil
+}
+
+func lexIdent(s string) (string, int, error) {
+	idx := strings.IndexAny(s, delimChars)
+	if idx == -1 {
+		return s, len(s), nil
+	} else {
+		return s[:idx], idx, nil
+	}
+}
+
+type lexEntry struct {
+	code TokenCode
+	fn   LexFn
+}
+
+var lexEntries = []lexEntry{
 
 Review comment:
   @utzig - Thanks, you are correct.  Originally I planned on using the single character forms for those operators, but I figured most users would be in the "C mindset", so I changed it.
   
   Unfortunately I can't fix the commit message since the PR has already been merged.  However, I will fix the comment in the code and file a new PR.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services