예제 #1
0
파일: main.go 프로젝트: nevet/gofile-parser
func Parse(buf *bufio.Scanner) (*GoFile, error) {
	state := StateIdle

	goFile := &GoFile{}

	for tokens, hasNext := utils.GetNextLine(buf); hasNext; tokens, hasNext := utils.GetNextLine(buf) {
		switch state {
		case StateIdle:
			goFile.Package = tokens[1]
			state = StateInPackage

		case StateInPackage:
			switch tokens[0] {
			case "import":
				goFile.Import = append(goFile.Import, parseImport(buf, tokens[1:])...)
			// case "const":
			// 	append(goFile.Const, parseOneOrMoreLine(buf, tokens))
			case "var":
				goFile.Var.Append(parseVar(buf, tokens[1:]))
			case "func":
				goFile.Functions = append(goFile.Functions, parseFunc(buf, tokens[1:]))
				// case "type":
				// 	append(goFile.Type, parseOneOrMoreLine(buf, tokens))
				// 	}
			}
		}
	}

	return goFile, nil
}
예제 #2
0
func (definition *Definition) Parse(buf *bufio.Scanner, lineTokens *[]string) {
	for (*lineTokens)[0] != ")" {
		if len(*lineTokens) == 1 {
			*lineTokens, _ = utils.GetNextLine(buf)
		} else if (*lineTokens)[0] == "," {
			*lineTokens = (*lineTokens)[1:]
		} else {
			definition.Append(parseDefinitionItem(lineTokens))
		}
	}

	*lineTokens = (*lineTokens)[1:]
}
예제 #3
0
func parseBody(buf *bufio.Scanner, lineTokens *[]string) (funcBody []string) {
	// since the function is well formatted, there are only 2 possible cases:
	// 1. { function_body }
	// 2.
	// { <--- must be on a new line with itself
	//     function_body
	// } <--- must be on a new line with itself

	// case 1
	funcBody = append(funcBody, *lineTokens...)

	// case 2
	if len(*lineTokens) == 1 {
		for *lineTokens, _ = utils.GetNextLine(buf); len(*lineTokens) != 1 || (*lineTokens)[0] != "}"; *lineTokens, _ = utils.GetNextLine(buf) {
			funcBody = append(funcBody, *lineTokens...)
		}
	}

	return
}
예제 #4
0
func TestDefinitionParse(t *testing.T) {
	var testData = []ParseTestCase{
		{
			name:           "Definition Parse Test 1 -- One Pair",
			data:           "(name type)",
			expectedResult: Definition{"type": []string{"name"}},
		},
		{
			name:           "Definition Parse Test 2 -- Two Pairs",
			data:           "(name string, another bool)",
			expectedResult: Definition{"string": []string{"name"}, "bool": []string{"another"}},
		},
		{
			name:           "Definition Parse Test 3 -- Two Pairs, Multiple Per Pair",
			data:           "(name, work string, age int)",
			expectedResult: Definition{"string": []string{"name", "work"}, "int": []string{"age"}},
		},
		{
			name: "Definition Parse Test 4 -- One Pair, Multiple Line",
			data: `(
					name string,
					)`,
			expectedResult: Definition{"string": []string{"name"}},
		},
		{
			name: "Definition Parse Test 5 -- Two Pairs, Multiple Line",
			data: `(
					name string,
					another int)`,
			expectedResult: Definition{"string": []string{"name"}, "int": []string{"another"}},
		},
		{
			name:           "Definition Parse Test 6 -- Type Aggregation In One Line",
			data:           `(name string, work string)`,
			expectedResult: Definition{"string": []string{"name", "work"}},
		},
		{
			name: "Definition Parse Test 7 -- Type Aggregation In Multiple Lines",
			data: `(
					name string,
					work string)`,
			expectedResult: Definition{"string": []string{"name", "work"}},
		},
	}

	for _, test := range testData {
		buf := bufio.NewScanner(strings.NewReader(test.data))
		tokens, _ := utils.GetNextLine(buf)
		definition := Definition{}

		definition.Parse(buf, &tokens)

		assert.Equal(t, test.expectedResult, definition, test.name)

		// tokens should possess only 1 item now
		assert.Equal(t, 0, len(tokens), test.name)

		// next buf.Scan should return false
		assert.Equal(t, false, buf.Scan(), test.name)
	}
}